ALTER PROCEDURE [dbo].[Link_OnlineCommand]
(
	@result tinyint output,
	@res varchar(1024) output,
	@jdev_type tinyint,
	@jdev_serial int,
	@jl_data_link varchar(8000)
)
AS

set nocount on;
declare @a int
declare @cmd_bz int
declare @remain_data varchar(8000)		--分隔后剩余字符串
declare @dev_serial varchar(20)
declare @card_hao varchar(20)
declare @user_serial bigint
declare @user_no nvarchar(50)
declare @user_lname nvarchar(50)
declare @user_depname nvarchar(50)
declare @ac_money int 					--账户余额
declare @ac_subo int 					--补贴余额
declare @time_no char(16)				--时段号
declare @ac_each int					--总分数
declare @sj varchar(20)					--时间字符串变量
declare @ac_stmoney int					--冻结总金额
declare @ac_stsubo int					--冻结补贴余额
declare @ac_steach int 					--冻结份数
declare @shiduan int
declare @ac_type varchar(20)
declare @shiduan_bh varchar(20)			--时段编号
declare @yich int
declare @day_max_m int 					--日限额
declare @day_max_t int					--日限次
declare @ac_time_offset int				--时段偏移量
declare @time_max_m int 				--单次限额
declare @current_date datetime 			--当前时间
declare @out_put3 int
declare @xh int							--变量存储临时表总条数
declare @dep_serial0 int				--设备直接对应的场所序号
declare @dep_serial2 int				--场所定义表中的场所序号
declare @dep_parent int					--场所对应的上级场所序号
declare @out_put int
declare @a1 varchar(32)     --16进制物理卡号
declare @a2 varchar(4)      --交易类型： 1:份消费、41:金额消费
declare @a3 varchar(24)     --终端设备编号
declare @a4 varchar(16)     --终端交易流水号
declare @a5 varchar(16)     --卡顺序号  --指纹识别时为人员序号
declare @a6 varchar(16)     --交易前余额
declare @a7 varchar(16)     --交易金额
declare @a8 varchar(16)     --交易日期 YYYYMMDD格式
declare @a9 varchar(12)     --交易时间 HHMMSS格式
declare @a10 varchar(12)    --卡交易计数器
declare @a11 varchar(20)     --卡认证码
declare @a12 varchar(12)    --补贴版本号
declare @a13 varchar(32)    --设备时段编号
declare @a14 varchar(32)    --个人时段编号
declare @a15 varchar(8)     --扣款类型
declare @a16 varchar(16)    --交易前补贴余额
declare @a17 varchar(16)    --补贴金额
declare @a18 varchar(16)    --应收金额
declare @a19 varchar(16)    --打折折数
declare @a20 varchar(16)    --交易前份数
declare @a21 varchar(16)    --交易份数
declare @a22 varchar(16)    --份单价
declare @a23 varchar(16)    --是否异常标志位
declare @a24 varchar(16)    --上次交易时段序号
declare @a25 varchar(16)    --本次交易时段序号
declare @a26 varchar(32)    --上次交易时段编号
declare @a27 varchar(16)    --识别介质
declare @a28 varchar(16)    --TAC校验码
declare @a29 varchar(12)    --交易密码
declare @a30 nvarchar(50)	--员工编号
declare @a31 nvarchar(50)   --员工姓名
declare @dev_serial1 varchar(10)
declare @dev_serial2 varchar(7)    --设备编号
declare @card_hao_dev varchar(16) --物理卡号
declare @a2_dev int               --交易类型
declare @a3_dev int               --设备编号
declare @a4_dev int               --交易流水号
declare @a5_dev int               --卡顺序号（在此为人员序号）
declare @aa7_dev int              --应收金额
declare @aa21_dev int             --交易份数
declare @a27_dev int              --识别介质
declare @a29_dev int              --交易密码
declare @state_flag int
declare @jl_data nvarchar(1024)
declare @card_serial char(8)--卡顺序号
declare @current_sj datetime 		--偏移后当前时间
declare @style int					--识别方式
declare @user_serial2 bigint
declare @day_account_t int			--日累计次数
declare @day_account_m int 			--日累计金额
declare @time_t int 				--时段限次
declare @time_account_t int			--时段次数
declare @time_m int 				--时段限额
declare @time_account_m int			--时段金额
declare @aa7 int   --应收金额/交易金额
declare @aa21 int  --交易份数
declare @ac_jlcount int
declare @dep_serial int         --部门序号
declare @mould int              --消费模式，1：份消费，0：金额消费
declare @jl_sj datetime         --请求数据域的时间
declare @reply_ok int
declare @each_lx int	--份记录类型
declare @dis_online int 	--是否存在脱机的账户 1代表存在；0代表不存在
declare @card_type int 		--卡号状态 卡号状态：0：正常，1：挂失，2：退卡，3：临时卡
declare @days int			--当月剩余天数
declare @pay_type int       --扣款类型
declare @discount_rate int  --打折折数
declare @discount_rate2 int --时段内折数
declare @ac_addo int        --账户现金余额
declare @more_money int     --应收金额
declare @jl_count int       --交易累计号
declare @time_order char(16)--设备时段编号
declare @rq varchar(16)     --交易日期
declare @rq_sj datetime     --存放偏移后的日期时间
declare @sub_no int         --补贴版本号
declare @sub_money int      --补贴金额
declare @dev_type int               --设备扣款类型
declare @change_ok int  --是否成功标志
declare @rate  money    --折扣
declare @pass_max int   --密码是否启用
declare @once_max_m int --账户单次限额
declare @time_limit int     --时段是否启用限额限次
declare @time_max_t int     --时段最大限次
declare @sj_dev datetime
declare @day_money int      --人员当前日消费额
declare @day_count int      --人员当前日消费次
declare @xf_money int       --人员当前时段消费额
declare @xf_count int       --人员当前时段消费次
declare @xf_each int        --人员当前时段份数
declare @user_Card varchar(20)
declare @shiduan_xh int
declare @sub_order int
declare @shiduan_ks int
declare @shiduan_js int
declare @meal_state int
declare @pass int
declare @pass_word int
declare @out_put2 int
declare @day_sub int
declare @user_time_no varchar(20)
declare @ac_state int               --联机账户状态 启用或停用
declare @ac_sub_auto int            --是否启用自动补贴
declare @dis_daysub int             --日补标志位
declare @dis_mealsub int            --餐补标志位
declare @sub int                    --自动补贴金额
declare @ac_time_state int          --账户类型是否允许非时段消费
declare @ac_time_type int           --自动补贴类型 0：日补贴，1：时段补贴
declare @ac_time_mould int          --时段补贴方式 0：时段定额补贴，1：消费多少补多少（有限额）
declare @time_sub int               --时段补贴金额
declare @ac_time_day int            --日补贴金额
declare @daysub_flag int            --本日是否已补贴
declare @hoursub_flag int           --本时段是否已补贴
declare @ac_kssj datetime           --人员账户开始时间
declare @ac_jssj datetime           --人员账户结束时间
declare @insert_yc int
declare @dev_serial_yc varchar(10)
declare @jl_bh_yc varchar(20)
declare @user_serial_yc bigint
declare @new_money_yc int
declare @more_money_yc int
declare @jl_number int
declare @each_unit int		--份单价
declare @sub_old int 		--交易前补贴余额
declare @ac_sub_rule int
declare @ac_sub_limit int
declare @ac_sub_unit int
declare @auto_kssj datetime
declare @auto_jssj datetime
declare @dev_lb int
declare @ac_subsidy int
declare @current_bh varchar(20)
declare @res_day_sub money --自动补贴金额
declare @res_sub_new money --撤销扣款中的补贴扣款金额
declare @res_sub_lx int	--撤销交易类型
declare @res_money money	--撤销金额 为负数金额
declare @res_shiduan varchar(20)	--撤销时段
declare @res_rq datetime 	--撤销日期
declare @res_jlcount int	--撤销累计号
declare @res_time_count int	--时段补贴标志位
declare @res_day_count int	--日补贴
declare @res_data varchar(1000)
declare @rollback_sub int
declare @card_in varchar(20)
declare @time_rq datetime
declare @meal_sub int
declare @ac_subm int              --补贴累计金额
declare @jl_shiduan int         --时钟偏移后的秒数
declare @mx_rq datetime 	    --明细中的日期，不包括时间
declare @current_rq  datetime 	--当前时间的日期
declare @current_shiduan int 	--当前时间的秒数
declare @new_money int 	        --实收金额
declare @time_xh int	           --时段序号
declare @ac_state1 int 	           --账户状态
declare @current_sjd varchar(20)   --中间变量
declare @add_money int             --个人账户扣款金额
declare @now_bh int                --设备流水号
declare @jl_bh  nvarchar(50)		--记录流水号
declare @new_each varchar(20)		--交易份数
declare @each_del varchar(20)		--份单价
declare @user_password varchar(20)	--个人密码
declare @back_ok int
declare @ac_blockm int
declare @ac_blocks int
declare @ac_blocke int
declare @command_type varchar(10)
declare @command_password varchar(10)
declare @dev_bh varchar(10)
declare @card_hao2 varchar(20)
declare @add_sj datetime
declare @add_mi varchar(20)
declare @add_ss int
declare @once_add_max money
declare @day_add_count int
declare @day_add_max money
declare @gly_kssj datetime
declare @gly_jssj datetime
declare @dev_count int
declare @dev_max money
declare @add_gly varchar(50)
declare @ac_each_auto int
declare @ac_timeeach_type int
declare @ac_timeeach_mould int
declare @ac_timeeach_day int
declare @cz_no varchar(50)

declare @sub_type int
declare @cz_name nvarchar(50)
declare @cp int

declare @sign_time int		--充值机签到时间间隔(单位秒)

set @cp=0




create table #temp(
	xh int identity(1,1),
	serial bigint
	)

set @result=0
set @res=''
set @dev_serial='001'+right('0000'+@jdev_serial,4)

select @sign_time=case when recharge_interval=0 then 1800 else recharge_interval end
from st_device where bh=@dev_serial
--1.解析字符串
set @a=charindex(',',@jl_data_link)
set @cmd_bz=left(@jl_data_link,@a-1)
set @remain_data=substring(@jl_data_link,@a+1,len(@jl_data_link)-@a)


if @cmd_bz=6000		--余额查询
begin


	set @out_put3=1

	----------------------------------------------分割字符串
	set @a=charindex(',',@remain_data)
	set @style=left(@remain_data,@a-1)		--识别方式
	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @card_hao=left(@remain_data,@a-1)	--物理卡号
	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @user_serial2=@remain_data			--人员序号
	----------------------------------------------初始化变量


	set @yich=0
	set @ac_money=0
	set @ac_stmoney=0
	set @ac_subo=0
	set @ac_addo=0
	set @ac_stsubo=0
	set @ac_each=0
	set @ac_steach=0
	set @time_max_m=0				--单次限额
	set @day_max_t=0				--日限次
	set @day_account_t=0			--日累计次数
	set @day_max_m=0				--日限额
	set @day_account_m=0			--日累计金额
	set @time_no='0000000000000000'	--时段号
	set @time_t=0					--时段限次
	set @time_account_t=0			--时段次数
	set @time_m=0					--时段限额
	set @time_account_m=0			--时段金额
	set @card_in=@card_hao
	set @card_hao=right('0000000000000000'+@card_hao,16)      --id原始卡号
	declare @dep_serial6000 varchar(20)
	------------------------------------------------------------------

	if(@style=113)
	begin
		select @user_serial=user_serial from dt_user
		where user_serial=@user_serial2
	end
	else--if(@style=147)	--id卡根据卡号取得人员序号
	begin
		select @user_serial=user_serial from dt_card
		where right('0000000000000000'+card_hao,16)=@card_hao
	end
-------------



--------------------------取人员对应的账户类型编号
	select @ac_type=ac_type from dt_ac_user where user_serial=@user_serial
	select @user_no=user_no,@user_lname=user_lname,@user_depname=user_depname
	from dt_user where user_serial=@user_serial
	------------------------------------------------------------------
	set @current_date=getdate()
	----取账户日限次与日限额
	select @day_max_t=day_max_t,@day_max_m=day_max_m,@ac_time_offset=ac_time_offset
	from dt_ac_type where ac_bh=@ac_type

	set @sj=CONVERT(varchar(100), @current_date, 24) 	--取当前时间变量
	set @sj=substring(@sj,1,2)+substring(@sj,4,2)	--转换成字符串变量
	+substring(@sj,7,2)
	set @shiduan=substring(@sj,1,2)*3600+substring(@sj,3,2)*60 --转换成秒数
	+substring(@sj,5,2)
	--------------------------计算偏移后的时段
		set @shiduan=@shiduan-@ac_time_offset
		if(@shiduan<0)
		begin
			set @shiduan=86400+@shiduan
			set @current_sj=dateadd(day,-1,@current_date)
		end
		else if(@shiduan>86400)
		begin
			set @shiduan=@shiduan-86400
			set @current_sj=dateadd(day,1,@current_date)
		end
		else
		begin
			set @current_sj=@current_date
		end
	--/////---
			select @shiduan_bh=a.bh
			from xf_time a   --消费时段表
			inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
			where @shiduan>=a.kssj and @shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0
	--取时段的限额限次
	select @time_m=time_max_m,@time_t=time_max_t from xf_ac_time where ac_type=@ac_type and time_no=@shiduan_bh and lx=0
	--取时段的累计额与累计次（包扩冻结的金额）
	select @dep_serial6000=acdep_serial from dt_dev where dev_serial=@dev_serial and lx=3
	---------------------------------------------(冻结金额暂设默认值)
	---判断系统中是否存在对应的联机消费设备
	if((select count(bh) from st_device where bh=@dev_serial and dev_lb=6)=0)
	begin
	set @yich=1
	set @out_put=24		--无此设备
	end
	------------------------
	-------------------------------------------------------------------------
	if(@yich!=1)
	begin
		if(select count(user_serial) from dt_user where user_serial=@user_serial and user_type<>51)=0 --无此人员
		begin
			set @out_put=6
		end
		else if (select count(1) from
			(

				select  a.user_serial
				from dt_ac_dep_user a
				inner join dt_user b on a.user_serial=b.user_serial
				where a.dep_serial=@dep_serial6000 and b.user_type<51
				and isnull(module_id,'0035')='0010'
				union all
				select  a.user_serial
				from dt_user a
				inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
				inner join dt_ac_dep f on b.ac_serial=f.dep_serial
				where f.dep_serial=@dep_serial6000
				and a.user_type<51
				)a1)<>0 and (
			select count(1) from
			(

				select  a.user_serial
				from dt_ac_dep_user a
				inner join dt_user b on a.user_serial=b.user_serial
				where a.dep_serial=@dep_serial6000 and b.user_type<51
				and isnull(module_id,'0035')='0010'
				union all
				select  a.user_serial
				from dt_user a
				inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
				inner join dt_ac_dep f on b.ac_serial=f.dep_serial
				where f.dep_serial=@dep_serial6000
				and a.user_type<51
				)a1 where a1.user_serial=@user_serial
			)=0
		begin
			--增加判断无权限卡，场所未指定白名单的卡 1.取设备对应场所的白名单(带继承关系)；2.
			set @out_put=26
		end
		-------/////////////////--卡片已挂失
		 else if(select xh from dt_card  where user_serial=@user_serial and right('0000000000000000'+card_hao,16)=@card_hao and card_type=1) is not null
		begin
			set @out_put=5
		end

		--///////////////////--账户已过有效期
		else if(select convert(varchar(15),ac_jssj,112) from dt_ac_user where user_serial=@user_serial)<convert(varchar(15),getdate(),112)
		begin
			set @out_put=3
		end
		--///////////////////--账户未启用
		else if(select user_serial from dt_ac_user where user_serial=@user_serial and ac_state=1) is not null
		begin
			set @out_put=2
		end

		--//////////////////--成功

		else
		begin
			--exec Pinsert_yz_sub @dev_serial,@user_serial,@out_put3 output     --处理补贴，已处理有效期，累计，周期等方式。
			select @ac_money=a.ac_money,
			@ac_stmoney=0,
			@ac_subo=a.ac_subo,
			@ac_addo=a.ac_addo,
			@ac_each=a.ac_eacho,
			@ac_steach=0,
			@time_max_m=b.time_max_m,
			@day_max_t=b.day_max_t,
			@day_max_m=b.day_max_m
			from dt_ac_link a with(nolock)
			inner join dt_ac_user c with(nolock) on c.user_serial=a.user_serial
			inner join dt_ac_type b on c.ac_type=b.ac_bh
			where a.user_serial=@user_serial

			--select @day_account_t=count(xh) ,@day_account_m=sum(new_money) from xf_jl	--取消费日累计金额与日累计次数
			--where user_serial=@user_serial and CONVERT(varchar(100), jl_sj, 112)=CONVERT(varchar(100), GETDATE(), 112) and lx=1
			set @time_no=isnull(@shiduan_bh,'0000000000000000')
			select @day_account_t=day_count,@day_account_m=day_money
			from xf_user_time
			where user_serial=@user_serial and rq=CONVERT(varchar(100), @current_sj, 112)


			select @time_account_t=xf_count,@time_account_m=xf_money
			from xf_user_time
			where user_serial=@user_serial and time_no=@time_no

			set @out_put=0
		end
		truncate table #temp
	end



	set @res=
	'6000'+','+
	cast(@out_put as varchar(10))+','+  							--返回码
	cast(@style as varchar(10))+','+								--识别方式
	@card_in+','+													--物理卡号
	cast(isnull(@user_serial,'') as varchar(20))+','+				--人员序号
	cast(isnull(@ac_money,0) as varchar(20))+','+					--账户余额
	cast(isnull(@ac_subo,0) as varchar(20))+','+					--
	cast(isnull(@ac_addo,0) as varchar(20))+','+					--补贴余额
	isnull(@user_depname,'')+','+									--冻结补贴额
	cast(isnull(@ac_each,0) as varchar(20))+','+					--账户份数
	'0'+','+														--账户冻结份数
	cast(isnull(@time_max_m,0) as varchar(20))+','+					--单次限额
	cast(isnull(@day_max_t,0) as varchar(20))+','+					--日限次
	isnull(cast(@day_account_t as varchar(20)),0)+','+				--日累计次数
	cast(isnull(@day_max_m,0) as varchar(20))+','+					--日限额
	isnull(cast(@day_account_m as varchar(20)),0)+','+				--日累计额
	cast(isnull(@time_no,'0000000000000000') as varchar(20))+','+	--时段编号
	cast(isnull(@time_t,0) as varchar(20))+','+						--时段限次
	cast(isnull(@time_account_t,0) as varchar(20))+','+				--时段累计次数
	cast(isnull(@time_m,0) as varchar(20))+','+						--时段限额
	cast(isnull(@time_account_m,0) as varchar(20))+','+				--时段累计额
	cast(isnull(@user_no,0) as nvarchar(50))+','+					--人员编号
	cast(isnull(@user_lname,0) as nvarchar(50))						--人员姓名
	--select @out_put	,@style,@card_in
	return
end

else if @cmd_bz=3000				--在线消费
begin
set @a=charindex(',',@remain_data)
set @a1=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a2=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a3=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a4=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a5=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a6=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a7=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a8=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a9=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a10=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a11=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a12=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a13=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a14=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a15=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a16=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a17=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a18=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a19=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a20=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a21=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a22=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a23=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a24=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a25=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a26=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a27=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a28=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a29=@remain_data

--set @a29=left(@remain_data,@a-1)
--set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

--set @a=charindex(',',@remain_data)
--set @a30=left(@remain_data,@a-1)
--set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

--set @a=charindex(',',@remain_data)
--set @a31=@remain_data

--限制消费开始 2020年3月20日 10:47:28 lzl
	DECLARE @userType int --考勤类型
	DECLARE @userId numeric --人员id
	DECLARE @shiftId int --排班班次ID (（非0，即为有排班）
	DECLARE @swipeBegin int --上班打卡时间（分钟数）
	DECLARE @swipeEnd int --下班打卡时间（分钟数）
	DECLARE @otBillId int --加班单ID （非0，即为有加班）
	DECLARE @otIsEating int --加班是否就餐（0否  1是）
	DECLARE @otBegin int --加班开始时间
	DECLARE @otEnd int --加班结束时间
	DECLARE @userXfType varchar(100) --账户类型
	DECLARE @str varchar(100) --加班结束时间
	DECLARE @flag int --满足条件个数
	DECLARE @countNum int --记录查询返回的条数

	set @flag = 0;
	select @userType = kqdkLx,@userId = user_serial from dt_user where user_card = @a1
	if @userType in (20,21,24)
	BEGIN
		select
			@shiftId = shift_id,
			@swipeBegin = swipe_begin,
			@swipeEnd = swipe_end,
			@otBillId = ot_bill_id,
			@otIsEating = ot_is_eating,
			@otBegin = ot_begin,
			@otEnd = ot_end
		from bd_kq_timecard
		where user_serial = @userId and convert(varchar(10),card_date,120) = convert(varchar(10),GETDATE(),120)
		--insert into aa(name)VALUES(@str)
		--当天有正常排班、打卡记录，并且在就餐时段内，则允许刷卡就餐
		if @shiftId !=0 and (@swipeBegin != 0 or @swipeEnd != 0)
		BEGIN
			--当前时间在班次对应的消费时间段内，就认为可以就餐
			select @countNum = count(1) from xf_time x
			INNER JOIN bd_banci_time_config b on x.bh = b.time_id
			where b.whether_deleted = 0
				and b.banci_id = @shiftId
				and dbo.bd_fn_datetimeToInt(GETDATE(), GETDATE())*60 BETWEEN x.kssj and x.jssj
			--积累满足条件返回的记录数
			set @flag = @flag+@countNum;
		END
		--当天有加班，并选择了加班就餐，根据加班时间内包含的就餐时间，与当前时间对比，判断是否允许就餐
		if @otBillId! = 0 and @otIsEating = 1
		BEGIN
			select @countNum = count(1) from xf_time
			where kssj>= @otBegin*60 and jssj<=  @otEnd*60
				and dbo.bd_fn_datetimeToInt(GETDATE(), GETDATE())*60 BETWEEN kssj and jssj

			--积累满足条件返回的记录数
			set @flag = @flag+@countNum;
		END

	END
	else BEGIN
			--若考勤类型不是20、21、24,则默认满足条件
			set @flag = 1
		END
		--先注释相关逻辑，之后根据客户需求
-- 	--如果该人员的类型为班次司机,则需要当天有打卡记录才能就餐
-- 	select @countNum = count(1) from  dt_ac_type t
-- 	INNER JOIN dt_ac_user u on t.ac_bh = u.ac_type
-- 	where u.user_serial = @userId  and t.ac_name like '%班车%'
-- 	if @countNum>0
-- 	BEGIN
-- 		--班车司机是一个独立的条件，需初始化@flag
-- 		set @flag = 0
-- 		--是班次司机，查询其打卡信息（次数）
-- 		select @countNum = count(1) from kt_jl
-- 		where user_serial = @userId and convert(varchar(10),sj,120) = convert(varchar(10),GETDATE(),120)
-- 		--积累满足条件返回的记录数
-- 		set @flag = @flag+@countNum;
-- 	END
	--满足条件的值若等于0,表示不满足就餐条件
	--insert into aa(name)VALUES(@flag)
	if @flag=0
	BEGIN
		set @out_put=25
		set @res='3000'+','+ cast(@out_put as varchar(10))
		return
	END
--限制消费结束 2020年3月20日 10:47:28 lzl


-------------------------------------------请求消费有效数据域
--物理卡号 @card_hao
--交易类型 @a2 1：金额消费、41：份消费
--设备编号 @a3
--交易流水号（记录流水号，按设备，顺序增加） @a4
--卡顺序号（在此为人员序号） @a5
--应收金额 @aa7
--交易份数 @aa21
--识别介质 @a27
--交易密码 @a29
-------------------------------------------相关数据类型转换
declare @time_eachsub int
declare @ac_each_subsidy int

set @a2=convert(int,@a2)        --记录类型
set @card_hao=right('0000000000000000'+@a1,16)      --转换为16位卡号,THEX格式
set @a27=convert(int,@a27)      --识别介质
set @dev_serial2='001'+right('0000'+@dev_serial,4)       --设备编号处理
set @aa7=convert(int,@a18)      --应收金额
set @aa21=convert(int,@a21)     --交易份数
--set @aa16=convert(int,@a16)     --终端未给数据，该值无效
--set @aa17=convert(int,@a17)     --补贴金额，终端未给数据
set @each_lx=@a2
--------------------------------区分记录模式：现金，份数
if (@a2=41 or @a2=11) --交易类型 01：消费，11:菜谱消费 02：充值，04：补贴，08：提现 16：
begin
   --补贴清零，32：冲正， 34：日补贴，35：时段补贴，41：份消费，42：份补贴，43：份清零，44：份冲正
	if(@a2=41)
	begin
    set @mould=1  --份消费
	set @a2=1
	end
	else
	begin
	set @mould=0
	set @a2=1
	set @cp=1
	end
end
else if(@a2=1 or @a2=12)
begin
    set @mould=0  --金额消费
end
-----------------------------------------终端给的消费时间
set @jl_sj=cast(substring(@a8,1,4)+'-'+substring(@a8,5,2)
    +'-'+substring(@a8,7,2)+' '+substring(@a9,1,2)
    +':'+substring(@a9,3,2)+':'+substring(@a9,5,2) as datetime)
------------------------------------------




set @insert_yc=0
set @daysub_flag=0
set @hoursub_flag=0
set @change_ok=0
set @yich=0
set @day_money=0
set @day_count=0
set @xf_money=0
set @xf_count=0
set @xf_each=0
set @pass=0
set @reply_ok=0
set @out_put3=1
set @auto_kssj=null
set @auto_jssj=null
set @dis_online=0

-----------------------------------------日期处理
set @current_date=getdate()            --统一日期时间
set @rq=CONVERT(varchar(100), @current_date, 112)
set @sj=CONVERT(varchar(100), @current_date, 24)
set @sj=substring(@sj,1,2)+substring(@sj,4,2)+substring(@sj,7,2)
set @shiduan=substring(@sj,1,2)*3600+substring(@sj,3,2)*60+substring(@sj,5,2) --转换成秒数
---判断系统中是否存在对应的联机消费设备
if((select count(bh) from st_device where bh=@dev_serial2 and dev_lb=6)=0)
begin
set @yich=1
set @out_put=24		--无此设备
end
---判断设备是否一致，设备是否停用
if(@yich!=1)
begin
select @dev_lb=dev_lb from st_device where cast(bh as int)=cast(@a3 as int)
if(cast(@dev_serial as int) != cast(@a3 as int) and (@dev_lb=99)) --判断设备编号是否一致,设备未删除
begin
	set @yich=1
	set @out_put=1
end
else
begin
    select @dev_type=dev_type,@pay_type=pay_type,@sub_type=sub_type
	from st_device where bh=@dev_serial2 --判断设备是否存在，存在取对应场所白名单权限
	set @a15=@pay_type
end
end

--begin transaction
SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
if(@yich!=1 and @a27=113) --指纹方式
begin
    select
        @user_serial=a.user_serial,	--人员序号
        @user_no=a2.user_no,		--人员编号
        @user_lname=a2.user_lname,	--人员姓名
        @user_depname=a2.user_depname,
        @card_serial=a.card_serial,     --卡顺序号
        @discount_rate=isnull(d.discount_rate,100), --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_state=d.ac_time_state, --账户类型是否允许非时段消费
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
        @ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=c.ac_state,           --账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=d.ac_time_day,     --日补贴金额
        @day_max_t=d.day_max_t,         --账户日限次
        @day_max_m=d.day_max_m,         --账户日限额
        @once_max_m=d.time_max_m,       --账户单次限额
        @ac_time_offset=d.ac_time_offset, --时段偏移量
        @pass_max=d.pass_max,            --密码是否启用
        @pass_word=c.ac_pass,            --密码
        @sub_order=a.sub_order,			 --当前补贴版本号
        @ac_kssj=c.ac_kssj,
        @ac_jssj=c.ac_jssj,                                 --卡状态
        @ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
        @ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
        @ac_sub_unit=d.ac_sub_unit,       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒（目前只用年月日）
        @ac_subsidy=d.ac_subsidy,		  --是否启用补贴
        @ac_each_auto=d.ac_each_auto,	  --是否启用份自动补贴
        @ac_timeeach_type=d.ac_timeeach_type,	--自动补贴类型
        @ac_timeeach_mould=d.ac_timeeach_mould,	--时段补贴模式
        @ac_timeeach_day=d.ac_timeeach_day,		--日补贴份
        @ac_each_subsidy=d.ac_each				--是否启用份消费
    from dt_ac_link a with(nolock)
    --inner join dt_card b on a.user_serial=b.user_serial   --卡号表， 不再需要
    inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
    inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表，取出折数
    inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
    where a.user_serial=@a5 and a2.user_type!=51     --人员序号

    if(@user_serial is null)    --人员不存在
    begin
        set @yich=1 --返回异常信息
        set @out_put=7
    end
end
else if (@yich!=1) --卡消费
begin
    select @user_serial=a.user_serial,  --人员序号
        @user_no=a2.user_no,
        @user_lname=a2.user_lname,
        @user_depname=a2.user_depname,
        @card_serial=b.card_serial,     --卡顺序号
        @discount_rate=isnull(d.discount_rate,0), --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_state=d.ac_time_state, --账户类型是否允许非时段消费
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
		@ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=c.ac_state,           --账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=isnull(d.ac_time_day,0),     --日补贴金额
        @day_max_t=d.day_max_t,         --账户日限次
        @day_max_m=d.day_max_m,         --账户日限额
        @once_max_m=d.time_max_m,       --账户单次限额
        @ac_time_offset=d.ac_time_offset,--时段偏移量
        @pass_max=d.pass_max,            --密码是否启用
        @pass_word=c.ac_pass,            --密码
        @sub_order=a.sub_order,          --当前补贴版本号
        @card_type=b.card_type,          --卡状态：0：正常，1：挂失
        @ac_kssj=c.ac_kssj,
        @ac_jssj=c.ac_jssj,
        @ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
        @ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
        @ac_sub_unit=d.ac_sub_unit,       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒
        @ac_subsidy=d.ac_subsidy,
        @ac_each_auto=d.ac_each_auto,
        @ac_timeeach_type=d.ac_timeeach_type,
        @ac_timeeach_mould=d.ac_timeeach_mould,
        @ac_timeeach_day=d.ac_timeeach_day,
        @ac_each_subsidy=d.ac_each
        from dt_ac_link a with(nolock)
        inner join dt_card b on a.user_serial=b.user_serial   --卡号表
        inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where right('0000000000000000'+b.card_hao,16)=@card_hao and a2.user_type!=51
    ----------------------------------------------------------------------人员信息预判断

    if(@user_serial is null)    --卡不存在
    begin
        set @yich=1     --返回异常信息
        set @out_put=6  --无此卡号
    end
    else if(@card_type=1)
    begin
        set @yich=1
        set @out_put=5 --挂失
    end

end
--增加判断无权限卡，场所未指定白名单的卡 1.取设备对应场所的白名单(带继承关系)；2.

select @dep_serial0=(select acdep_serial from dt_dev where dev_serial=@dev_serial2 and lx=3)
select @dep_serial2=(select dep_serial from dt_ac_dep where dep_serial=@dep_serial0)
select @dep_parent=(select dep_parent from dt_ac_dep where dep_serial=@dep_serial2)
if(@yich!=1)
begin



	insert into #temp(serial)
		select distinct user_serial from
		(
		select  user_serial
		from dt_ac_dep_user
		where dep_serial=@dep_serial2
		and isnull(module_id,'0035')='0010'
		union all
		select  a.user_serial
		from dt_user a
		inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
		inner join dt_ac_dep f on b.ac_serial=f.dep_serial
		where f.dep_serial=@dep_serial2
		) a

	select @xh=(select count(*) from #temp a inner join dt_user b on a.serial=b.user_serial and b.user_type<51)  --取人员的个数，以判断是否存在白名单


	if((@xh!=0) and (select count(xh) from #temp where serial=@user_serial)=0)
	begin
	set @yich=1
	set @out_put=26
	end
	truncate table #temp
end
---------------------------------------
if(@yich!=1)
begin


    --账户未启用 --在此为账户注销 账户状态：0：启用，1：停用
    --@ac_kssj datetime           --人员账户开始时间
    --@ac_jssj datetime           --人员账户结束时间

	if (@ac_state = 1)
    begin
        set @yich=1
        set @out_put=2 --账户停用
    end
    else if(convert(varchar(20),@ac_jssj,112) < convert(varchar(20),@current_date,112) or (convert(varchar(20),@ac_kssj,112)) > (convert(varchar(20),@current_date,112)))
    begin
        set @yich=1
        set @out_put=3 --人员账户不在有效期
    end
end



------处理撤销消费流程
------记账模式下的撤销处理方式
--记账消费下的撤销金额处理，若扣款模式为先扣补贴再扣现金，此时处理撤销记录的时候，若账户补贴余额还有剩余，
--说明该部分撤销的金额应返回给补贴；若账户补贴余额为0，此时记账的撤销金额默认返还给现金
--相反，若扣款模式为先现金再补贴的模式，此时处理撤销记录的时候，若账户存在现金，说明该部分应返回给现金账户，
--若为0 则返回给补贴账户
------自动补贴的撤销
--对于有自动补贴的消费记录，同时需要撤销自动补贴
--
------////////////////////////--------------------
if(@a2=31 and @yich!=1)
begin
		set @shiduan=@shiduan-@ac_time_offset

		if(@shiduan<0)
		begin
			set @shiduan=86400+@shiduan
			set @rq_sj=dateadd(day,-1,@current_date)
		end
		else if(@shiduan>86400)
		begin
			set @shiduan=@shiduan-86400
			set @rq_sj=dateadd(day,1,@current_date)
		end
		else
		begin
			set @rq_sj=@current_date
		end

		----------------------取账户类型的限额限次
		--@shiduan_bh varchar(20)     --时段编号
		--@discount_rate2 int         --时段内折数
		--@shiduan_xh int             --时段序号
		--@shiduan_ks
		--@shiduan_js
		--@time_limit int             --时段是否启用限额限次
		--@time_max_m int             --时段的最大限额
		--@time_max_t int             --时段最大限次
		--@time_sub int               --时段补贴金额
		set @shiduan_bh = null
		select
		@shiduan_bh=a.bh,	--时段编号
		@discount_rate2=isnull(b.discount_rate,0),	--时段折扣
		@shiduan_xh=b.day_order,		--序号
		@shiduan_ks=a.kssj,				--开始时间
		@shiduan_js=a.jssj,				--结束时间
		@time_limit=b.time_limit,		--是否启用限额限次
		@time_max_m=b.time_max_m,		--时段限额
		@time_max_t=b.time_max_t,		--时段限次
		@time_sub=isnull(b.time_sub,0)	--时段补贴
		from xf_time a   --消费时段表
		inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
		where @shiduan>=a.kssj and @shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0
		--@time_order --设备时段编号
		--@each_unit  --份单价
		select
		@time_order=b.time_no,
		@each_unit=isnull(b.each_unit,0)
		from xf_time a   --消费时段表
		inner join xf_ac_time b on b.time_no=a.bh   --设备对应的时段表
		inner join dt_dev c on c.acdep_serial=b.acdep_serial
		where @shiduan>=a.kssj and @shiduan<a.jssj and c.dev_serial=@dev_serial2 and b.lx=2
--判断是否存在对应的消费记录（与设备最后一条记录相比对）
if exists (select * from xf_jl_yc with(nolock) where dev_serial=@a3 and user_serial=@user_serial
and jl_bh=@a4-1)	--缺少金额比对
begin
--变量定义

--变量赋值
set @rollback_sub=0
select  @res_money=-new_money from xf_jl_yc where dev_serial=@a3
select @res_shiduan=time_no,@res_rq=jl_sj,@res_jlcount=jl_count,@res_sub_new=sub_new,
@res_sub_lx=res_sub_lx,
@res_day_sub=res_day_sub
from xf_jl_yc with(nolock) where dev_serial=@a3

select @res_time_count=sub_hour,@res_day_count=sub_day from xf_user_time
where user_serial=@user_serial

--1.撤销统计信息（累计金额、次数）
if exists (select * from xf_user_time where user_serial=@user_serial
and convert(varchar(20),rq,23)=convert(varchar(20),@res_rq,23))--存在当前日期累计
begin	--更新日统计信息
update xf_user_time set day_money=day_money+@res_money,day_count=day_count-1
where user_serial=@user_serial and convert(varchar(20),rq,23)=convert(varchar(20),@res_rq,23)
end
if exists (select * from xf_user_time where user_serial=@user_serial
and convert(varchar(20),rq,23)=convert(varchar(20),@res_rq,23) and time_no=@res_shiduan) --存在当前时段累计
begin	--更新时段统计信息
update xf_user_time set xf_money=xf_money+@res_money,xf_count=xf_count-1
where user_serial=@user_serial and convert(varchar(20),rq,23)=convert(varchar(20),@res_rq,23)
and time_no=@res_shiduan
end
--2.撤销自动补贴（时段补贴、日补贴）*根据消费记录的累积号判断上一条记录的累积号 与交易类型*
--select @res_sub_lx=lx,@res_day_sub=new_sub from xf_mx
--where jl_count=@res_jlcount-1 and user_serial=@user_serial
if @res_sub_lx=34
begin
set @res_day_count=0
end
else if @res_sub_lx=35
begin
set @res_time_count=0
end
else
begin
set @rollback_sub=1
end
if(@rollback_sub!=1)	--联机消费 使用当前时间作为消费记录的时间
begin
			insert into xf_jl       --生成撤销自动补贴交易记录
				(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,
				card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,sub_old,sub_new,each_old,
				each_new,each_unit,more_money,discount_rate,gly_no)
			values(0,@res_sub_lx,'0000000000',@current_date,0,@ac_money,-@res_day_sub,@jl_count,@dev_serial2,@user_serial,@dep_serial,
				@card_serial,@current_date,@card_hao,0,0,4,'0000000000000000','0000000000000000',0,@ac_subo,-@res_day_sub,
				0,0,0,0,100,null)

			insert into xf_mx       --生成撤销自动补贴明细记录
				(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
				old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
				new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
				new_each,each_unit,save_add,save_sub,save_money,jl_count,dev_time_no,time_no,gly_no,sj,acdep_serial)
			values(0,@res_sub_lx,1,'00000000',@dev_serial2,@user_serial,@dep_serial,@user_card,@card_serial,convert(varchar(20),
				@current_date,23),@current_date,
				@ac_addo,@ac_subo,@ac_money,0,0,0,100,0,
				-@res_day_sub,1,0,0,0,0,0,0,0,0,0,0,@ac_addo,@ac_subo-@res_day_sub,
				@ac_money-@res_day_sub,@jl_count,'0000000000000000','0000000000000000',null,@current_date,@dep_serial2)

			update dt_ac_link set   --更新联机账户
				ac_money=ac_money-isnull(@res_day_sub,0),
				ac_subm=ac_subm-isnull(@res_day_sub,0),
				ac_subo=ac_subo-isnull(@res_day_sub,0),
				jl_count=@jl_count
				where user_serial=@user_serial

			update dt_Ac_user set   --更新人员总账户
				ac_money=ac_money-isnull(@res_day_sub,0),
				ac_subm=ac_subm-isnull(@res_day_sub,0),
				ac_subo=ac_subo-isnull(@res_day_sub,0)
				where user_serial=@user_serial



			set @ac_money = @ac_money - isnull(@res_day_sub,0)           --联机账户总余额
			set @ac_subo = @ac_subo - isnull(@res_day_sub,0)             --联机账户补贴余额
			set @jl_count=@jl_count+1

			update xf_user_time set sub_day=@res_day_count,sub_hour=@res_time_count
			where user_serial=@user_serial
end

--3.生成撤销记录 （补贴撤销记录<上一步完成>、消费撤销记录）
			insert into xf_jl   --将终端读取的记录插入原始记录表
				(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,
				dep_serial,card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,
				sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate)
			values(0,1,@a4,@current_date,0,@ac_money,@res_money,@jl_count,@dev_serial2,@user_serial,
				@dep_serial,@card_serial,@current_date,@card_hao,@a12,0,4,isnull(@time_order,'0000000000000000'),isnull(@time_order,'0000000000000000'),@a15,
				@ac_subo,-@res_sub_new,@ac_each,@a21,isnull(@each_unit,0),cast(@a18 as money),isnull(@discount_rate,100))

				insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,xf_add,xf_sub,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,sj,acdep_serial)
				values(0,1,1,@a4,@dev_serial2,@user_serial,@dep_serial,@card_hao,@card_serial,convert(varchar(20),@current_date,23),@current_date,
					@ac_addo,@ac_subo,@ac_money,@res_money,@res_money,@res_money+@res_sub_new,-@res_sub_new,0,isnull(@discount_rate,100),1,
					0,0,0,0,0,0,0,0,0,0,@a21,isnull(@each_unit,0),@ac_addo-(@res_money+@res_sub_new),@ac_subo+@res_sub_new,
					@ac_money-@res_money,@jl_count,isnull(@time_order,'0000000000000000'),isnull(@time_order,'0000000000000000'),@current_date,@dep_serial0)

--4.还原账户金额 （联机账户、总账户）
	update dt_ac_link set
		ac_money=isnull(ac_money,0)-isnull(@res_money,0),                             --联机总账户余额
		ac_addo=isnull(ac_addo,0)-(isnull(@res_money,0)+isnull(@res_sub_new,0)),        --个人账户余额
		ac_subo=isnull(ac_subo,0)+isnull(@res_sub_new,0),                         --补贴账户余额
		--ac_blockm=isnull(ac_blockm,0)+(isnull(@res_money,0)+isnull(@res_sub_new,0)),    --个人账户冻结金额
		--ac_blocks=isnull(ac_blocks,0)+isnull(@res_sub_new,0),                     --补贴账户冻结金额
		jl_count=@jl_count                                                      --卡片交易计数
	where user_serial=@user_serial

--			update dt_ac_link set
--				ac_money=isnull(ac_money,0)-isnull(@res_money,0),                             --联机总账户余额
--				ac_addo=isnull(ac_addo,0)-(isnull(@res_money,0)+isnull(@res_sub_new,0)),        --个人账户余额
--				ac_subo=isnull(ac_subo,0)-isnull(@res_sub_new,0),                         --补贴账户余额
--				jl_count=@jl_count                                                      --卡片交易计数
--			where user_serial=@user_serial

			update dt_ac_user set
				ac_money=isnull(ac_money,0)-isnull(@res_money,0),                             --联机总账户余额
				ac_addo=isnull(ac_addo,0)-(isnull(@res_money,0)+isnull(@res_sub_new,0)),        --个人账户余额
				ac_subo=isnull(ac_subo,0)+isnull(@res_sub_new,0)                        --补贴账户余额                                                   --卡片交易计数
			where user_serial=@user_serial

			set @ac_subo = @ac_subo +isnull(@res_sub_new,0)
			set @ac_addo=@ac_addo - (isnull(@res_money,0)+isnull(@res_sub_new,0))

--5.返回成功
set @out_put=0

delete from xf_menu_jl
where dev_serial=@dev_serial2 and jl_serial=@a4-1

set @res_data='3000'+','+
				cast(@out_put as varchar(10))+','+
				cast(@a1 as varchar(20))+','+
				cast(@each_lx as varchar(4))+','+
				isnull(@a3,'')+','+
				isnull(@a4,'')+','+
				cast(@user_serial as varchar(20))+','+
				isnull(cast(@ac_money as varchar(20)),'')+','+
				cast(@res_money as varchar(20))+','+
				@rq+','+
				@sj+','+
				isnull(@user_depname,'')+','+
				isnull(@a11,'')+','+
				isnull(@a12,'')+','+
				isnull(@a13,'')+','+
				isnull(@shiduan_bh,'0000000000000000')+','+
				isnull(cast(@a15 as varchar(10)),'')+','+
				isnull(cast(@sub_money as varchar(20)),0)+','+
				isnull(cast(@res_money-@sub_money as varchar(20)),0)+','+
				isnull(cast(@a18 as varchar(20)),0)+','+
				isnull(cast(@discount_rate as varchar(20)),'')+','+
				isnull(cast(@ac_each as varchar(10)),0)+','+
				isnull(cast(@a21 as varchar(10)),0)+','+
				isnull(cast(@each_unit as varchar(10)),0)+','+
				'0'+','+
				'0'+','+
				isnull(cast(@ac_subo as varchar(20)),0)+','+
				isnull(cast(@ac_addo as varchar(20)),0)+','+
				cast(@a27 as varchar(10))+','+
				isnull(@a28,'')+','+
				cast(isnull(@a29,0) as varchar(10)) +','+
				isnull(@user_no,'')+','+
				isnull(@user_lname,'')

			--set @jl_number=@out_put
			--update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number where user_serial=@user_serial and dev_serial=@a3

			set @res=@res_data
			return
end
--与设备最后一条记录不匹配
else	--异常情况
begin
set @yich=1
set @out_put=29	--该笔交易无法纠错,与设备最后一条记录不匹配
	set @res_data='3000'+','+
        cast(@out_put as varchar(10))+','+
        cast(@a27 as varchar(10))+','+
        isnull(@a1,0)+','+
        isnull(cast(@user_serial as varchar(20)),0)+','+
        isnull(@a4,0)+','+
        isnull(cast(@ac_money as varchar(20)),0)+','+
        isnull(cast(@ac_stmoney as varchar(20)),0)+','+
        isnull(cast(@ac_subo as varchar(20)),0)+','+
        isnull(cast(@ac_stsubo as varchar(20)),0)+','+

        isnull(cast(@ac_each as varchar(20)),0)+','+

        isnull(cast(@ac_steach as varchar(20)),0)

	--set @jl_number=@out_put
	--update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number where user_serial=@user_serial and dev_serial=@a3

	set @res=@res_data
	return
end
return
end






------查询是否存在其他的未处理的消费请求
--冻结状态表 一台设备对应一条记录
--字段：设备编号、冻结标志（0：未冻结，1：冻结）、返回的记录内容

--物理卡号 @card_hao
--交易类型 @a2 1：金额消费、41：份消费
--设备编号 @a3
--交易流水号（记录流水号，按设备，顺序增加） @a4
--卡顺序号（在此为人员序号） @a5
--应收金额 @aa7
--交易份数 @aa21
--识别介质 @a27
--交易密码 @a29
----------------------------------------------

if(@yich!=1)
begin

	set @state_flag = null
	select @state_flag=state,@card_hao_dev=user_card, @a2_dev=lx, @a3_dev=dev_serial,@a4_dev=jl_bh,
		@a5_dev=user_serial,@aa7_dev=more_money,@aa21_dev=each_new,@a27_dev=ident_type,@a29_dev=ac_pass,@jl_number=jl_number,@jl_data =jl_data,
		@sj_dev=jl_sj
	from xf_jl_yc with(nolock) where dev_serial=@a3

	if (@state_flag is null)
	begin

		--插入记录
		insert into xf_jl_yc(jl_sj,dev_serial,jl_bh,user_serial,user_card,lx,state,more_money,each_new,ident_type,ac_pass,now_bh)
		values(@jl_sj,@a3,@a4,@user_serial,@a1,@each_lx,1,cast(@a18 as int),@a21,@a27,isnull(@a29,0),0)

		 --给变量赋值
		set @state_flag=1
		set @card_hao_dev=@a1
		set @a2_dev=@each_lx
		set @a3_dev=@a3
		set @a4_dev=@a4
		set @a5_dev=@user_serial
		set @aa7_dev=cast(@aa7 as int)
		set @aa21_dev=@a21
		set @a27_dev=@a27
		set @a29_dev=@a29

	end
	else if(@state_flag<>0)
	begin

	 if ( @user_serial = @a5_dev and @each_lx = @a2_dev and @a4 = @a4_dev
	 and @aa7 = @aa7_dev and @aa21 = @aa21_dev and @a27 = @a27_dev and @a29 = @a29_dev
	 and @sj_dev=@jl_sj
	 ) --重复请求
	 begin  --重复请求

		set @yich=2
		set @reply_ok=1
		set @change_ok=1

	 end
	 /*
	 else if(@a3=@a3_dev and @a4-@a4_dev<1)
	 begin

		insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,state_flag,new_money,new_each,dev_type,pass,make_lx,t_level,style_yc)
		values(@a3_dev,@a4_dev,@a5_dev,@card_hao_dev,@a2_dev,@state_flag,@aa7_dev,@aa21_dev,@a27_dev,@a29_dev,3,3,3)
		set @yich=0

	 end
	 */

	end

------------------------------------------------------------------------
	--@day_max_t int
	--@day_max_m int
	--@once_max_m int
	--@ac_time_offset int
	--@pass_max int
	if(@yich!=1 and @yich!=2)
	begin

		set @shiduan=@shiduan-@ac_time_offset

		if(@shiduan<0)
		begin
			set @shiduan=86400+@shiduan
			set @rq_sj=dateadd(day,-1,@current_date)
		end
		else if(@shiduan>86400)
		begin
			set @shiduan=@shiduan-86400
			set @rq_sj=dateadd(day,1,@current_date)
		end
		else
		begin
			set @rq_sj=@current_date
		end

		----------------------取账户类型的限额限次
		--@shiduan_bh varchar(20)     --时段编号
		--@discount_rate2 int         --时段内折数
		--@shiduan_xh int             --时段序号
		--@shiduan_ks
		--@shiduan_js
		--@time_limit int             --时段是否启用限额限次
		--@time_max_m int             --时段的最大限额
		--@time_max_t int             --时段最大限次
		--@time_sub int               --时段补贴金额
		set @shiduan_bh = null
		select
		@shiduan_bh=a.bh,
		@discount_rate2=isnull(b.discount_rate,0),
		@shiduan_xh=b.day_order,
		@shiduan_ks=a.kssj,
		@shiduan_js=a.jssj,
		@time_limit=b.time_limit,
		@time_max_m=b.time_max_m,
		@time_max_t=b.time_max_t,
		@time_sub=isnull(b.time_sub,0),			--时段补贴
		@time_eachsub=isnull(b.time_eachsub,0),	--时段补份
		@ac_time_mould=ac_time_mould,			--时段补贴模式
		@ac_timeeach_mould=ac_eachtime_mould	--时段份补模式
		from xf_time a   --消费时段表
		inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
		where @shiduan>=a.kssj and @shiduan<a.jssj
		and b.ac_type=@ac_type and b.lx=0
		--@time_order --设备时段编号
		--@each_unit  --份单价
		select @time_order=b.time_no,@each_unit=isnull(b.each_unit,0) from xf_time a   --消费时段表
		inner join xf_ac_time b on b.time_no=a.bh   --设备对应的时段表
		inner join dt_dev c on c.acdep_serial=b.acdep_serial
		where @shiduan>=a.kssj and @shiduan<a.jssj and c.dev_serial=@dev_serial2 and b.lx=2
		----------------------------------------------------------------------根据折扣计算实收金额
		if(@shiduan_bh is not null)  --时段内的打折
		begin
			set @discount_rate=@discount_rate2
		end
		if(@cp=0 and @dev_type<>1)--20181030 lrh 调整特殊消费机不参与打折
		begin
		set @rate=convert(money,@discount_rate)
		set @aa7=@aa7*(@rate/100)       --打折后金额
		end
--------------------------------账户时段内禁止消费

--20180727 lrh 新增消费请求时先处理冲正请求beg
		declare @cz_xh int
		declare @cz_jldata varchar(2000)
		declare @cz_mould int
		declare @cz_money money
		declare @cz_timeno char(16)

		set @cz_xh=0
		select  top 1 @cz_xh=xh from link_offline_cz where user_serial=@user_serial and deal_state=0
		if @cz_xh>0
		begin
			select @cz_jldata=jl_data from link_offline_cz where xh=@cz_xh
			exec link_offline_czdeal @cz_jldata,0,@cz_xh
		end

--20180727 lrh 新增消费请求时先处理冲正请求end



if(@dev_type<>1)
begin	---6
	   if(@time_limit=2)	--时段内禁止消费
	   begin
	   set @yich=1
	   set @out_put=25
	   end
	   else
	   begin	---3
	   --begin transaction
--SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
		--卡片已挂失 卡户状态：0：正常，1：挂失，2：退卡，3：已提现

		----------数据预处理
		---1.账户额，时段额，时段次，日限额，日限次
		---2.时段偏移量，处理当前时间应所处时段
		---3.账户折扣，计算实收金额
		---4.账户补贴允许位以及补贴方式

		-------------------------日期或时段发生变化 人员状态置初始值

		if(@mould=0)	--金额模式
		begin
			select @user_time_no=time_no
			from xf_user_time where user_serial=@user_serial

			--20180727 lrh 处理完冲正后调整限额限次

			select @cz_mould=mould,@cz_money=new_money,@cz_timeno=time_no from link_offline_cz
			where xh=@cz_xh
			and deal_state=1
			and convert(varchar(7),jl_sj,120)=convert(varchar(7),getdate(),120)
			and user_serial=@user_serial

			if isnull(@cz_mould,3)=0
			begin
				update xf_user_time set day_money=(case when isnull(day_money,0)>0 then day_money-@cz_money else day_money end),
										day_count=(case when isnull(day_count,0)>0 then day_count-1 else day_count end)
				where user_serial=@user_serial

				if @user_time_no=@cz_timeno
				begin
					update xf_user_time set xf_money=(case when isnull(xf_money,0)>0 then xf_money-@cz_money else xf_money end),
											xf_count=(case when isnull(xf_count,0)>0 then xf_count-1 else xf_count end)
					where user_serial=@user_serial

				end
			end

			--20180727 lrh 处理完冲正后调整限额限次

			select @time_rq=rq,@user_time_no=time_no,@daysub_flag=sub_day, @hoursub_flag=sub_hour,
			@day_money=day_money,@day_count=day_count,@xf_money=xf_money,@xf_count=xf_count
			from xf_user_time where user_serial=@user_serial

		end
		else if(@mould=1)	--份模式
		begin
			select @user_time_no=time_no
			from xf_user_time where user_serial=@user_serial

			--20180727 lrh 处理完冲正后调整限额限次
			select @cz_mould=mould,@cz_money=new_money,@cz_timeno=time_no from link_offline_cz
			where xh=@cz_xh
			and deal_state=1
			and convert(varchar(7),jl_sj,120)=convert(varchar(7),getdate(),120)
			and user_serial=@user_serial

			if isnull(@cz_mould,3)=1
			begin
				update xf_user_time set day_money=(case when isnull(day_money,0)>0 then day_money-@cz_money else day_money end),
										day_count=(case when isnull(day_count,0)>0 then day_count-1 else day_count end)
				where user_serial=@user_serial

				if @user_time_no=@cz_timeno
				begin
					update xf_user_time set xf_money=(case when isnull(xf_money,0)>0 then xf_money-@cz_money else xf_money end),
											xf_count=(case when isnull(xf_count,0)>0 then xf_count-1 else xf_count end)
					where user_serial=@user_serial

				end
			end
			--20180727 lrh 处理完冲正后调整限额限次

			select @time_rq=rq,@user_time_no=time_no,@daysub_flag=isnull(each_day,0), @hoursub_flag=isnull(each_hour,0),
			@day_money=day_money,@day_count=day_count,@xf_money=xf_money,@xf_count=xf_count
			from xf_user_time where user_serial=@user_serial

		end



		if(@time_rq is null) --人员账户累计记录不存在时 插入
		begin
			insert into xf_user_time(user_serial,rq,day_count,day_money,time_no,time_xh,time_ks,time_js,xf_count,xf_money,xf_each,sub_day,sub_hour,each_day,each_hour)
				values(@user_serial,convert(varchar(20),@rq_sj,23),case when @mould=0 then 1 else @aa21 end,@aa7,isnull(@shiduan_bh,'0000000000000000'),
				isnull(@shiduan_xh,0),@shiduan_ks,@shiduan_js,case when @mould=0 then 1 else @aa21 end,@aa7,@aa21,0,0,0,0)
			set @daysub_flag=0
			set @hoursub_flag=0
			set @day_money=0
			set @day_count=0
			set @xf_money=0
			set @xf_count=0
		end
		else
		begin	---2
			if(convert(varchar(20),@rq_sj,23)!=convert(varchar(20),@time_rq,23)) --日期变化
			begin
				update xf_user_time set
				day_count=case when @mould=0 then 1 else @aa21 end,
				day_money=@aa7,
				xf_count=case when @mould=0 then 1 else @aa21 end,
				xf_money=@aa7,
				sub_day=0,  --日已补贴标志
				sub_hour=0, --时段已补贴标志
				time_no=isnull(@shiduan_bh,'0000000000000000'),
				rq=convert(varchar(20),@rq_sj,23),
				each_day=0,
				each_hour=0
				where user_Serial=@user_Serial

				set @daysub_flag=0     --本日是否已补贴
				set @hoursub_flag=0    --本时段是否已补贴
				set @day_money=0
				set @day_count=0
				set @xf_money=0        --人员当前时段消费额
				set @xf_count=0
			end
			/*
			else if(convert(varchar(20),@rq_sj,23)=convert(varchar(20),@time_rq,23) and @shiduan_bh!=@user_time_no)  --只是时段变化
			begin
				update xf_user_time set
				xf_count=1,
				xf_money=@aa7,
				sub_hour=0, --时段已补贴标志
				time_no=isnull(@shiduan_bh,'0000000000000000')
				where user_Serial=@user_Serial

				set @hoursub_flag=0   --本时段是否已补贴
				set @xf_money=0       --人员当前时段消费额
				set @xf_count=0
			end
			*/

			if(convert(varchar(20),@time_rq,23)=convert(varchar(20),@rq_sj,23)) --当前日期与记录日期（偏移后）同
			begin	---1
				select @current_bh=a.bh  --当前时段编号
				from xf_time a           --消费时段表
				inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
				where @shiduan>=a.kssj and @shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0

				if (@current_bh = null)
				begin
					set @current_bh = '0000000000000000'
				end

				if (@current_bh =@user_time_no) --时段同
				begin
					if(@mould=0)  --金额
					begin
						update xf_user_time set
								day_count=day_count+1,
								day_money=day_money+@aa7,
								xf_count=xf_count+1,
								xf_money=xf_money+@aa7
						where user_serial=@user_serial
					end
					else if(@mould=1) --份
					begin
						update xf_user_time set
							day_count=day_count+isnull(@aa21,0),
							xf_count=xf_count+isnull(@aa21,0)
						where user_serial=@user_serial
					end
				end
				else --时段不同
				begin
					if(@mould=0) --金额
					begin
					--select @current_bh ,@user_time_no
						update xf_user_time set
							day_count=day_count+1,
							day_money=day_money+@aa7,
							xf_count=1,
							xf_money=@aa7,
							time_no=@current_bh,
							sub_hour=0
						where user_serial=@user_serial

						--set @day_money=0
						--set @day_count=0
						set @xf_money=0        --人员当前时段消费额
						set @xf_count=0
						set @hoursub_flag=0

					end
					else if(@mould=1)  --份
					begin
						update xf_user_time set
							day_count=day_count+isnull(@aa21,0),
							xf_count=isnull(@aa21,0),
							time_no=@current_bh,
							each_hour=0
						where user_serial=@user_serial
						set @xf_count=0
						set @hoursub_flag=0
					end
				end
			end	---1

		end	---2
	end	---3
		--commit transaction

--SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚

		--1.处理手工发放的补贴（份、现金补贴）
		--exec Pinsert_yz_sub @dev_serial2,@user_serial,@out_put3     --已处理有效期，累计，周期等方式。

		if(@out_put3=1) --补贴或补贴清零，重新取账户金额
		begin
			select --根据手动补贴的返回值进行处理 也可直接计算，不进行查询
				@ac_money=isnull(a.ac_money,0),           --账户总余额
				@ac_addo=isnull(a.ac_addo,0),             --账户现金余额
				@ac_subo=isnull(a.ac_subo,0),             --账户补贴余额
				@ac_each=isnull(a.ac_eacho,0),             --份数余额
				@jl_count=a.jl_count+1,
				@sub_order=a.sub_order
			from dt_ac_link a with(nolock) where a.user_serial=@user_serial
		end

		---1.2查询当前的人员的最后一条补贴版本号
		--select @sub_order=sub_order from dt_ac_link where user_serial=@user_serial
		if(@a29 is not null and @a29!=0 and @pass_max=1)  --密码启用，输入密码
		begin
			if(@a29=@pass_word)
			begin
				set @pass=1  --密码正确
			end
			else if(@a29!=@pass_word)
			begin
				set @yich=1  --密码错误
				set @out_put=23
				set @pass=2  --密码输入错误
			end
		end
		-----2.判断账户限额限次（加表存储账户的当前的状态）
		if(@dev_type=0 and @pass=0) ----未输入密码或密码未启用，且启用时段限制消费设备
		begin	---5
			if(@mould=0)    --现金消费模式 限额限次的处理
			begin	---4

				if(@once_max_m<@aa7 and @once_max_m!=0)     --单次限额
				begin
					set @yich=1
					set @out_put=13

					if(@pass_max=1) --密码启用
					begin
						set @out_put=18
					end
				end
				else if(@day_max_t<@day_count+1 and @day_max_t!=0)  --日限次
				begin
					set @yich=1
					set @out_put=16

					if(@pass_max=1)  --密码启用
					begin
						set @out_put=21
					end
				end
				else if(@day_max_m<@day_money+@aa7 and @day_max_m!=0) --日限额
				begin
					set @yich=1
					set @out_put=17

					if(@pass_max=1)  --密码启用
					begin
						set @out_put=22
					end
				end
				else if(isnull(@shiduan_bh,'')='') --非时段内
				begin
			---------账户非时段内可以消费 若终端设备对应时间有时段关系 账户时段 使用设备时段
					if(@ac_time_state=1)
					begin
						set @shiduan_bh=isnull(@time_order,'0000000000000000')
					end
			------------------------------
					else if (@ac_time_state = 0)                      --账户类型是否允许非时段消费
					begin
						set @yich=1
						set @out_put=12
					end
				end
				--20200114 lrh 新增场所时段关系判断
				else if (isnull(@time_order,'')='')
				begin
					set @yich=1
					set @out_put=12
				end
				else  --当前处于账户时段内，判断时段限额限次
				begin
					if(@time_limit=1 and (@xf_count+1) > @time_max_t and @time_max_t<>0)  --启用时段限额限次并且超限次
					begin
						set @yich=1
						set @out_put=14

						if(@pass_max=1)
						begin
							set @out_put=19
						end
					end
					else if(@time_limit=1 and (@xf_money+@aa7)>@time_max_m and @time_max_m<>0)
					begin
						set @yich=1
						set @out_put=15

						if(@pass_max=1)
						begin
							set @out_put=20
						end
				   end
				end
			end		---4
			else --份消费模式限次的处理
			begin	---4
			 if(isnull(@shiduan_bh,'')='' or isnull(@time_order,'')='') --非时段内--20190111 lrh调整份消费时段限次判断从原来的场所时段改成账户时段
				begin
				---------账户非时段内可以消费 若终端设备对应时间有时段关系 账户时段 使用设备时段
					--if(@ac_time_state=1)
					--begin
--						set @shiduan_bh=isnull(@time_order,'0000000000000000')
					--end
					--else if (@ac_time_state = 0)                      --账户类型是否允许非时段消费
					--begin
						set @yich=1
						set @out_put=12
					--end
				end
				else if(@day_max_t<@day_count + @aa21 and @day_max_t != 0)  --日限次
				begin
					set @yich=1
					set @out_put=16

					if(@pass_max=1)  --密码启用
					begin
						set @out_put=21
					end
				end
				else if(@time_limit=1 and (@xf_count+@aa21) > @time_max_t) --启用时段限额限次并且超限次
				begin
					set @yich=1
					set @out_put=14

					if(@pass_max=1)
					begin
						set @out_put=19
					end
				end
			end --份消费模式限次的处理4
		end --限额限次 5
	end	---6
	-----------------------------------------------4.账户余额判断（加上餐补金额一块判断）
	-----------------------------------------------4.1账户余额够用，返回成功
	--select @yich,@mould,@ac_sub_auto,@ac_time_type,@ac_time_mould

	/*
		if(@yich!=1 and @yich!=2 and @mould=0 and @ac_sub_auto=1) --非异常、金额消费、自动补贴
		begin


			if(@ac_time_type=1)   --时段补贴
			begin
				if (@ac_time_mould = 0)  --时段定额补贴
				begin
					set @meal_sub = @time_sub
				end
				else if (@ac_time_mould = 1) --消费多少补多少，最大为时段定额
				begin
					if (@aa7 <= @time_sub)
					begin
						set @meal_sub = @aa7
					end
					else
					begin
						set @meal_sub = @time_sub
					end
				end

				set @sub=@meal_sub
			end  --时段补贴
			else --日补贴
			begin
				set @day_sub = @ac_time_day
				set @sub=@day_sub
			end
			--假设还未补贴
			if(@aa7>@ac_subo+@sub and @a15=1) or (@aa7>@ac_addo and @a15=2) or(@aa7>@ac_money+@sub)  --账户额度不足，返回失败，不进行时段补贴
			begin  --@a15:扣款类型 1：只扣补贴 2：只扣个人
				set @meal_state=0   --不补贴
			end
			else --账户额度够用，返回成功，进行时段补贴
			begin
				set @meal_state=1  --补贴允许（如果已补则不再补贴）
			end
		end
		--------------自动份
		else if(@yich!=1 and @yich!=2 and @mould=1 and @ac_each_auto=1) --非异常、金额消费、自动补贴
		begin


			if(@ac_timeeach_type=1)   --时段补贴
			begin
				if (@ac_timeeach_mould = 0)  --时段定额补贴
				begin
					set @meal_sub = @time_eachsub
				end
				else if (@ac_timeeach_mould = 1) --消费多少补多少，最大为时段定额
				begin
					if (@aa21 <= @time_eachsub)
					begin
						set @meal_sub = @aa21
					end
					else
					begin
						set @meal_sub = @time_eachsub
					end
				end

				set @sub=@meal_sub
			end  --时段补贴
			else --日补贴
			begin
				set @day_sub = @ac_timeeach_day
				set @sub=@day_sub
			end
			--假设还未补贴
			if(@aa21>@ac_each+@sub)  --账户额度不足，返回失败，不进行时段补贴
			begin  --@a15:扣款类型 1：只扣补贴 2：只扣个人
				set @meal_state=0   --不补贴
			end
			else --账户额度够用，返回成功，进行时段补贴
			begin
				set @meal_state=1  --补贴允许（如果已补则不再补贴）
			end
		end
	*/
		if(@yich!=1 and @yich!=2 and @mould=0 ) --非异常、金额消费、时段内未补贴
		begin


			if(@hoursub_flag=0)   --时段内未补贴
			begin
				if (@ac_time_mould = 0)  --时段定额补贴
				begin
					set @meal_sub = @time_sub
				end
				else if (@ac_time_mould = 1) --消费多少补多少，最大为时段定额
				begin
					if (@aa7 <= @time_sub and @time_sub<>0) or (@time_sub=0)
					begin
						set @meal_sub = @aa7
					end
					else
					begin
						set @meal_sub = @time_sub
					end
				end

				--set @sub=@meal_sub
			end  --时段补贴
			if(@daysub_flag=0) --日补贴未补
			begin
				set @day_sub = @ac_time_day 	--日补贴金额
				--set @sub=isnull(@day_sub,0)+isnull(@meal_sub,0)	--自动补贴总额（日补贴+时段补贴）
			end

			set @sub=isnull(@day_sub,0)+isnull(@meal_sub,0)
			--假设还未补贴
			if((@aa7>@ac_subo+@sub and @a15=1) or
			(@aa7>@ac_addo and @a15=2) or
			(@aa7>@ac_money+@sub))  --账户额度不足，返回失败，不进行时段补贴
			begin  --@a15:扣款类型 1：只扣补贴 2：只扣个人
				set @meal_state=0   --不补贴
			end
			else --账户额度够用，返回成功，进行时段补贴
			begin
				set @meal_state=1  --补贴允许（如果已补则不再补贴）
			end
		end
		--------------自动份
		else if(@yich!=1 and @yich!=2 and @mould=1) --非异常、金额消费、自动补贴
		begin


			if(@hoursub_flag=0)   --时段补贴
			begin
				if (@ac_timeeach_mould = 0)  --时段定额补贴
				begin
					set @meal_sub = @time_eachsub
				end
				else if (@ac_timeeach_mould = 1) --消费多少补多少，最大为时段定额
				begin
					if (@aa21 <= @time_eachsub and @time_eachsub<>0) or (@time_eachsub=0)
					begin
						set @meal_sub = @aa21
					end
					else
					begin
						set @meal_sub = @time_eachsub
					end
				end

				--set @sub=@meal_sub
			end  --时段补贴
			if(@daysub_flag=0) --日补贴
			begin
				set @day_sub = @ac_timeeach_day 	--日补份数
				--set @sub=@day_sub
			end

			set @sub=isnull(@day_sub,0)+isnull(@meal_sub,0)
			--假设还未补贴
			if(@aa21>@ac_each+@sub)  --账户额度不足，返回失败，不进行时段补贴
			begin  --@a15:扣款类型 1：只扣补贴 2：只扣个人
				set @meal_state=0   --不补贴
			end
			else --账户额度够用，返回成功，进行时段补贴
			begin
				set @meal_state=1  --补贴允许（如果已补则不再补贴）
			end
		end
		--------------自动份
		-------------------------------------------5.处理日补、餐补（将补贴金额写入账户），同时生产请求消费返回码
		-------------------------------------------5.1消费判断成功，进行日补餐补
		--存在脱机账户时  不进行联机自动补（暂未处理）
		--if((select count(xh) from dt_ac_card where user_serial=@user_serial)!=0)
		--begin
			--set @dis_online=1	--存在脱机的账户
		--end


		if(@mould=0)
		begin
			/*
			if(@dev_type=0 and @sub_type=0 and @dis_online=0 and @ac_subsidy=1
			and @ac_sub_auto=1 and @meal_state=1 and @yich!=1 and @yich!=2
			and @ac_time_type=0 and @day_sub!=0) --允许补贴、非异常、日补贴、日补贴金额、日补贴未补
			*/
			if(@day_sub<>0 and @yich<>1 and @yich<>2 and @sub_type=0)	--日补贴不为0；无异常；设备允许自动补贴
			begin

			/*
				if(@ac_sub_rule=1)		--补贴规则：0：补贴累计 1：周期补贴
				begin
					if(@ac_sub_limit=0)
					begin
						set @auto_kssj=dateadd(day,-1,@current_date)
						set @auto_jssj=dateadd(day,-1,@current_date)
					end
					else
					begin
						set @auto_kssj=@current_date
						if(@ac_sub_unit=0)  --年
						begin
							set @auto_jssj=dateadd(year,@ac_sub_limit,@current_date)
						end
						else if(@ac_sub_unit=1)  --月
						begin
							set @auto_jssj=dateadd(month,@ac_sub_limit-1,@current_date)
							select @days=(32-DAY(@auto_jssj+32-DAY(@auto_jssj))-cast(right(CONVERT(varchar(100), @current_date, 23),2) as int))
							set @auto_jssj=dateadd(day,@days,@auto_jssj)
						end
						else if(@ac_sub_unit=2)  --日
						begin
							set @auto_jssj=dateadd(day,@ac_sub_limit-1,@current_date)
						end
					end
				end
				*/
				insert into xf_jl       --生成日补贴交易记录
					(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,
					card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,sub_old,sub_new,each_old,
					each_new,each_unit,more_money,discount_rate,gly_no)
				values(0,34,'0000000000',@current_date,0,@ac_money,@day_sub,@jl_count,@dev_serial2,@user_serial,@dep_serial,
					@card_serial,@current_date,@card_hao,0,0,4,'0000000000000000','0000000000000000',0,@ac_subo,@day_sub,
					0,0,0,0,100,null)

				insert into xf_mx       --生成明细记录
					(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
					new_each,each_unit,save_add,save_sub,save_money,jl_count,dev_time_no,time_no,gly_no,sj,acdep_serial)
				values(0,34,1,'00000000',@dev_serial2,@user_serial,@dep_serial,@user_card,@card_serial,convert(varchar(20),
					@current_date,23),@current_date,
					@ac_addo,@ac_subo,@ac_money,0,0,0,100,0,
					@day_sub,1,0,0,0,0,0,0,0,0,0,0,@ac_addo,@ac_subo+@day_sub,
					@ac_money+@day_sub,@jl_count,'0000000000000000','0000000000000000',null,@current_date,@dep_serial0)


				update dt_ac_link set   --更新联机账户
					ac_money=ac_money+isnull(@day_sub,0),
					ac_subm=ac_subm+isnull(@day_sub,0),
					ac_subo=ac_subo+isnull(@day_sub,0),
					jl_count=@jl_count,
					sub_order=@sub_order+1
					where user_serial=@user_serial

				update dt_Ac_user set   --更新人员总账户
					ac_money=ac_money+isnull(@day_sub,0),
					ac_subm=ac_subm+isnull(@day_sub,0),
					ac_subo=ac_subo+isnull(@day_sub,0)
					where user_serial=@user_serial



				set @ac_money = @ac_money + isnull(@day_sub,0)           --联机账户总余额
				set @ac_subo = @ac_subo + isnull(@day_sub,0)             --联机账户补贴余额
				set @jl_count=@jl_count+1
				set @res_sub_lx=34
				set @res_day_sub=isnull(@day_sub,0)

				update xf_user_time set sub_day=1
				where user_serial=@user_serial --更新人员交易累计表的日补贴状态

			end
			/*
			else if(@dev_type=0 and @sub_type=0 and @dis_online=0 and @ac_subsidy=1
			and @ac_sub_auto=1 and @meal_state=1 and @yich!=1 and @yich!=2
			and @ac_time_type=1 and @meal_sub!=0 and @hoursub_flag=0) --餐补
			*/
			if(@dev_type=0 and @sub_type=0 and @yich!=1 and @yich!=2
				and @meal_state=1 and @meal_sub!=0)	--时段设备、允许自动补、非异常
			begin
			--select @meal_sub
			--select @dis_online,@ac_subsidy, @ac_sub_auto, @meal_state, @yich, --@ac_time_type, @meal_sub, @daysub_flag
			/*
				if(@ac_sub_rule=1)		--
				begin
					if(@ac_sub_limit=0)
					begin
						set @auto_kssj=dateadd(day,-1,@current_date)
						set @auto_jssj= dateadd(day,-1,@current_date)
					end
					else
					begin
						set @auto_kssj=@current_date
						if(@ac_sub_unit=0)
						begin
							set @auto_jssj=dateadd(year,@ac_sub_limit,@current_date)
						end
						else if(@ac_sub_unit=1)	--前月有效
						begin
							set @auto_jssj=dateadd(month,@ac_sub_limit-1,@current_date)
							select @days=(32-DAY(@auto_jssj+32-DAY(@auto_jssj))-cast(right(CONVERT(varchar(100), @current_date, 23),2) as int))
							set @auto_jssj=dateadd(day,@days,@auto_jssj)
						end
						else if(@ac_sub_unit=2)
						begin
							set @auto_jssj=dateadd(day,@ac_sub_limit-1,@current_date)
						end
					end
				end
				*/
				insert into xf_jl       --生成原始记录
					(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,
					dev_serial,user_serial,dep_serial,card_serial,sj,user_card,sub_xh,
					state,type,time_order,time_no,del_type,
					sub_old,sub_new,each_old,each_new,each_unit,more_money,
					discount_rate,gly_no
					)values(
					0,35,'0000000000',@current_date,0,@ac_money,isnull(@meal_sub,0),@jl_count,
					@dev_serial2,@user_serial,@dep_serial,@card_serial,@current_date,@card_hao,0,
					0,4,@time_order,@shiduan_bh,0,
					@ac_subo,isnull(@meal_sub,0),0,0,0,0,
					100,null)

				insert into xf_mx       --生成明细记录
					(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,
					rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,gly_no,sj,acdep_serial)
					values(0,35,1,'00000000',@dev_serial2,@user_serial,@dep_serial,@user_card,@card_serial,
					convert(varchar(20),@current_date,23),@current_date,
					@ac_addo,@ac_subo,@ac_money,0,0,0,100,0,
					isnull(@meal_sub,0),1,0,0,0,0,0,0,0,0,0,0,@ac_addo,@ac_subo+isnull(@meal_sub,0),
					@ac_money+isnull(@meal_sub,0),@jl_count,@time_order,@shiduan_bh,null,@current_date,@dep_serial0)


				update dt_ac_link set   --更新联机账户
					ac_money=ac_money+isnull(@meal_sub,0),
					ac_subm=ac_subm+isnull(@meal_sub,0),
					ac_subo=ac_subo+isnull(@meal_sub,0),
					jl_count=@jl_count,
					sub_order=@sub_order+1
					where user_serial=@user_serial

				update dt_Ac_user set   --更新人员总账户
					ac_money=ac_money+isnull(@meal_sub,0),
					ac_subm=ac_subm+isnull(@meal_sub,0),
					ac_subo=ac_subo+isnull(@meal_sub,0)
					where user_serial=@user_serial



				set @ac_money = @ac_money + isnull(@meal_sub,0)          --联机账户总余额
				set @ac_subo = @ac_subo + isnull(@meal_sub,0)             --联机账户补贴余额
				set @jl_count=@jl_count+1
				set @res_sub_lx=35
				set @res_day_sub=isnull(@meal_sub,0)

				update xf_user_time set sub_hour=1
				where user_serial=@user_serial  --更新人员交易累计表的时段补贴状态

			end  --自动补贴处理完成

		end

		else if(@mould=1)
		begin
			/*
			if(@dev_type=0 and @sub_type=0 and @ac_each_subsidy=1 and @ac_each_auto=1 and @meal_state=1 and @yich!=1 and @yich!=2
				and @ac_timeeach_type=0 and @day_sub!=0 and @daysub_flag=0)
				--允许补贴、非异常、日补贴、日补贴金额、日补贴未补
			*/
			if(@sub_type=0 and @day_sub<>0 and @yich!=1 and @yich!=2)
			begin

				insert into xf_jl       --生成日补贴交易记录
					(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,
					card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,sub_old,sub_new,each_old,
					each_new,each_unit,more_money,discount_rate,gly_no)
				values(1,34,'0000000000',@current_date,0,@ac_money,0,@jl_count,@dev_serial2,@user_serial,@dep_serial,
					@card_serial,@current_date,@card_hao,0,0,4,'0000000000000000','0000000000000000',0,0,0,
					@ac_each,@day_sub,0,0,100,null)

				insert into xf_mx       --生成明细记录
					(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
					new_each,each_unit,save_add,save_sub,save_money,jl_count,dev_time_no,time_no,gly_no,sj,acdep_serial)
				values(1,34,1,'00000000',@dev_serial2,@user_serial,@dep_serial,@user_card,@card_serial,convert(varchar(20),
					@current_date,23),@current_date,@ac_addo,@ac_subo,
					@ac_each,0,0,0,100,0,
					@day_sub,1,0,0,0,0,0,0,0,0,0,0,@ac_addo,@ac_subo,
					@ac_each+@day_sub,@jl_count,'0000000000000000','0000000000000000',null,@current_date,@dep_serial0)


				update dt_ac_link set   --更新联机账户
					ac_eacho=ac_eacho+cast(isnull(@day_sub,0) as int)
					where user_serial=@user_serial

				update dt_Ac_user set   --更新人员总账户
					ac_eacho=ac_eacho+cast(isnull(@day_sub,0) as int)
					where user_serial=@user_serial



				--set @ac_money = @ac_money + isnull(@sub,0)           --联机账户总余额
				--set @ac_subo = @ac_subo + isnull(@sub,0)             --联机账户补贴余额
				set @ac_each=@ac_each+cast(isnull(@day_sub,0) as int)
				set @jl_count=@jl_count+1
				set @res_sub_lx=34
				set @res_day_sub=isnull(@sub,0)

				update xf_user_time set each_day=1 where user_serial=@user_serial --更新人员交易累计表的日补贴状态

			end
			/*
			if(@dev_type=0 and @sub_type=0 and @ac_each_subsidy=1 and @ac_each_auto=1 and @meal_state=1
			and @yich!=1 and @yich!=2 and @ac_timeeach_type=1 and @meal_sub!=0 and @hoursub_flag=0) --份餐补
			*/
			if(@dev_type=0 and @sub_type=0 and @yich!=1 and @yich!=2 and @meal_state=1 and @meal_sub!=0)
			begin

				insert into xf_jl       --生成原始记录
					(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,
					dev_serial,user_serial,dep_serial,card_serial,sj,user_card,sub_xh,
					state,type,time_order,time_no,del_type,
					sub_old,sub_new,each_old,each_new,each_unit,more_money,
					discount_rate,gly_no
					)values(
					1,35,'0000000000',@current_date,0,@ac_money,0,@jl_count,
					@dev_serial2,@user_serial,@dep_serial,@card_serial,@current_date,@card_hao,0,
					0,4,@time_order,@shiduan_bh,0,
					@ac_subo,0,@ac_each,isnull(cast(@meal_sub as int),0),0,0,
					100,null)

				insert into xf_mx       --生成明细记录
					(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,
					rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,gly_no,sj,acdep_serial)
					values(1,35,1,'00000000',@dev_serial2,@user_serial,@dep_serial,@user_card,@card_serial,
					convert(varchar(20),@current_date,23),@current_date,@ac_addo,@ac_subo,
					@ac_each,0,0,0,100,0,
					isnull(@meal_sub,0),1,0,0,0,0,0,0,0,0,0,0,@ac_addo,@ac_subo,
					@ac_each+isnull(@meal_sub,0),@jl_count,@time_order,@shiduan_bh,null,@current_date,@dep_serial0)


				update dt_ac_link set   --更新联机账户
					ac_eacho=ac_eacho+isnull(cast(@meal_sub as int),0)
					where user_serial=@user_serial

				update dt_Ac_user set   --更新人员总账户
					ac_eacho=ac_eacho+isnull(cast(@meal_sub as int),0)
					where user_serial=@user_serial




				set @ac_each=@ac_each+isnull(cast(@meal_sub as int),0)
				set @jl_count=@jl_count+1
				set @res_sub_lx=35
				set @res_day_sub=isnull(@sub,0)

				update xf_user_time set each_hour=1 where user_serial=@user_serial  --更新人员交易累计表的时段补贴状态

			end  --自动补贴处理完成
		end

	end

	if(@yich!=1 and @yich!=2) --开始处理消费
	begin

		set @sub_old=@ac_subo
		if(@mould=0)    --现金消费模式
		begin
		--select @a15
			if(@a15=0)--先扣补贴后扣账户
			begin
				if(@ac_money<@aa7) --判断是否超额
				begin
					set @yich=1
					set @out_put=10
				end
				else
				begin
					if(@ac_subo<@aa7 and @ac_subo>0) --补贴余额小于消费金额
					begin
						set @ac_addo=@ac_addo-(@aa7-@ac_subo)
						set @sub_money=@ac_subo
						set @ac_subo=0
					end
					else if(@ac_subo<@aa7 and @ac_subo<0)	--补贴余额为负时
					begin
						set @ac_addo=@ac_addo-@aa7
						set @sub_money=0
					end
					else if(@ac_subo<@aa7 and @ac_subo=0)
					begin
						set @ac_addo=@ac_addo-@aa7
						set @sub_money=0
					end
					else --补贴够扣款
					begin
						set @ac_subo=@ac_subo-@aa7
						set @sub_money=@aa7
					end

					set @change_ok=1    --交易正常
					set @out_put=0
				end
			end
			else if(@a15=1)--只扣补贴
			begin
				if(@ac_subo<@aa7)
				begin
					set @yich=1
					set @out_put=8
				end
				else
				begin
					set @ac_subo=@ac_subo-@aa7
					set @sub_money=@aa7
					set @change_ok=1    --交易正常
					set @out_put=0
				end
			end
			else if(@a15=2)    --只扣个人账户
			begin
				if(@ac_addo<@aa7)
				begin
					set @yich=1
					set @out_put=10
				end
				else
				begin
					set @sub_money=0
					set @ac_addo=@ac_addo-@aa7
					set @change_ok=1    --交易正常
					set @out_put=0
				end
			end
			else if(@a15=3) --先个人后补贴
			begin
				if(@ac_money<@aa7) --判断是否超额
				begin
					set @yich=1
					set @out_put=10
				end
				else
				begin
					if(@ac_addo<@aa7) --个人余额小于消费金额
					begin
						set @ac_subo=@ac_subo-(@aa7-@ac_addo)
						set @sub_money=@aa7-@ac_addo
						set @ac_addo=0
					end
					else --个人够扣款
					begin
						set @ac_addo=@ac_addo-@aa7
						set @sub_money=0
					end

					set @change_ok=1    --交易正常
					set @out_put=0
				end
			end
		end
		else if(@mould=1) --份模式
		begin
			if(@ac_each<@aa21)
			begin
				set @yich=1
				set @out_put=11
			end
			else
			begin
				--set @ac_each=@ac_each-@aa21 --交易后份数，此时还未进行交易
				set @out_put=0
				set @change_ok=1
			end
		end

	end

end

--------------返回结果----------------
if((@yich=0) or (@yich=2))
begin

	if(@change_ok=1)
	begin

		if(@reply_ok=1)  --重复请求
		begin

			--select @jl_number,@jl_data
			set @res=@jl_data
			return
		end
		else  --新的请求
		begin
			--set @jl_count=@jl_count+1
			-----将记录表插入临时记录表
				--select
				--    @ac_money=a.ac_money,           --账户总余额
				--    @ac_addo=a.ac_addo,             --账户现金余额
				--    @ac_subo=a.ac_subo,             --账户补贴余额
				--    @ac_each=a.ac_eacho,            --账户份数余额
				--    @jl_count=a.jl_count+1         --卡交易次数
				--from dt_ac_link a where user_serial=@user_serial

			update xf_jl_yc
			set mould=@mould,type=4,discount_rate=@discount_rate,old_money=@ac_money,time_order=@time_order,time_no=@shiduan_bh,
				del_type=@a15,dev_serial=@a3,jl_bh=@a4,user_serial=@user_serial,user_card=@a1,lx=@each_lx,state=1,
				new_money=@aa7,more_money=cast(@a7 as int),each_new=@a21,ident_type=@a27,ac_pass=isnull(@a29,0),
				sub_old=@ac_subo,sub_new=@sub_money,dep_serial=@dep_serial,each_unit=isnull(@each_unit,0),card_serial=@card_serial,each_old=@ac_each,jl_count=@jl_count,jl_sj=@jl_sj,
				jl_data=null,now_bh=@a4,res_sub_lx=@res_sub_lx,res_day_sub=@res_day_sub
			where  dev_serial=@a3


-----------------请求时生成冻结记录 lx=36 冻结 lx=37 解冻
			insert into xf_jl   --将终端读取的记录插入原始记录表
				(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,
				dep_serial,card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,
				sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate)
			values(@mould,case when @a2=12 then 12 else 1 end,@a4,@current_date,0,@ac_money,@aa7,@jl_count,@dev_serial2,@user_serial,
				@dep_serial,@card_serial,@current_date,@card_hao,@a12,0,4,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@a15,
				@sub_old,@sub_money,@ac_each,@aa21,case when @mould=0 then 0 else isnull(@each_unit,0) end,cast(@a18 as money),@discount_rate)
-------不生成明细 记录确认后生成明细
			if(@mould=0)
			begin
				insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,xf_add,xf_sub,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,sj,acdep_serial)
				values(@mould,@a2,1,@a4,@dev_serial2,@user_serial,@dep_serial,@card_hao,@card_serial,convert(varchar(20),@current_date,23),@current_date,
					@ac_addo+(@aa7-@sub_money),@ac_subo+@sub_money,@ac_money,cast(@a18 as money),@aa7,@aa7-isnull(@sub_money,0),isnull(@sub_money,0),@a18-@aa7,@discount_rate,1,
					0,0,0,0,0,0,0,0,0,0,@aa21,isnull(@each_unit,0),
					@ac_addo,@ac_subo,@ac_money-@aa7,@jl_count,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@current_date,@dep_serial0)
			end
			else if(@mould=1)
			begin
				insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,each_count,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,sj,acdep_serial)
				values(@mould,@a2,1,@a4,@dev_serial2,@user_serial,@dep_serial,@card_hao
				,@card_serial,convert(varchar(20),@current_date,23),@current_date,
					@ac_addo,@ac_subo,@ac_each,@ac_money,@aa21*isnull(@each_unit,0),0,@discount_rate,1,
					0,0,0,0,0,0,0,0,0,0,@aa21,isnull(@each_unit,0),1,@ac_addo,@ac_subo,
					@ac_each-@aa21,@jl_count,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@current_date,@dep_serial0)
			end


			-----更新联机账户，冻结金额，总账户金额不变-----
			if(@mould=1)  --份模式
			begin
				update dt_ac_link set
					ac_eacho=isnull(ac_eacho,0)-isnull(cast(@a21 as int),0),    --份余额
					--ac_blocke=isnull(ac_blocke,0)+isnull(cast(@a21 as int),0),  --冻结份数
					jl_count=@jl_count                                          --卡片交易计数
				where user_serial=@user_serial
				update dt_ac_user set
				ac_eacho=isnull(ac_eacho,0)-isnull(cast(@a21 as int),0)
				where user_serial=@user_serial
			end
			else  --金额模式
			begin
				update dt_ac_link set
					ac_money=isnull(ac_money,0)-isnull(@aa7,0),                             --联机总账户余额
					ac_addo=isnull(ac_addo,0)-(isnull(@aa7,0)-isnull(@sub_money,0)),        --个人账户余额
					ac_subo=isnull(ac_subo,0)-isnull(@sub_money,0),                         --补贴账户余额
					--ac_blockm=isnull(ac_blockm,0)+(isnull(@aa7,0)-isnull(@sub_money,0)),    --个人账户冻结金额
					--ac_blocks=isnull(ac_blocks,0)+isnull(@sub_money,0),                     --补贴账户冻结金额
					jl_count=@jl_count                                                      --卡片交易计数
				where user_serial=@user_serial

				update dt_ac_user set
					ac_money=isnull(ac_money,0)-isnull(@aa7,0),                             --联机总账户余额
					ac_addo=isnull(ac_addo,0)-(isnull(@aa7,0)-isnull(@sub_money,0)),        --个人账户余额
					ac_subo=isnull(ac_subo,0)-isnull(@sub_money,0)
				where user_serial=@user_serial
			end

			--set @ac_subo=@ac_subo-isnull(@sub_money,0)
			--set @ac_addo=@ac_addo-(isnull(@aa7,0)-isnull(@sub_money,0))
-------------------------------------------------
-------------------------------------------------

-------------------------------------------------
-------------------------------------------------




			set @jl_data='3000'+','+
				cast(@out_put as varchar(10))+','+
				cast(@a1 as varchar(20))+','+
				cast(@each_lx as varchar(4))+','+
				isnull(@a3,'')+','+
				isnull(@a4,'')+','+
				cast(@user_serial as varchar(20))+','+
				isnull(cast(@ac_money as varchar(20)),'')+','+
				cast(@aa7 as varchar(20))+','+
				@rq+','+
				@sj+','+
				isnull(@user_depname,'')+','+		--调整成部门
				isnull(@a11,'')+','+
				isnull(@a12,'')+','+
				isnull(@a13,'')+','+
				isnull(@shiduan_bh,'0000000000000000')+','+
				isnull(cast(@a15 as varchar(10)),'')+','+
				isnull(cast(@sub_money as varchar(20)),0)+','+
				isnull(cast(@aa7-@sub_money as varchar(20)),0)+','+
				isnull(cast(@a18 as varchar(20)),0)+','+
				isnull(cast(@discount_rate as varchar(20)),'')+','+
				isnull(cast((@ac_each-@aa21) as varchar(10)),0)+','+
				isnull(cast(@a21 as varchar(10)),0)+','+
				isnull(cast(@each_unit as varchar(10)),0)+','+
				'0'+','+
				'0'+','+
				isnull(cast(@ac_subo as varchar(20)),0)+','+
				isnull(cast(@ac_addo as varchar(20)),0)+','+
				cast(@a27 as varchar(10))+','+
				isnull(@a28,'')+','+
				cast(isnull(@a29,0) as varchar(10)) +','+
				isnull(@user_no,'')+','+
				isnull(@user_lname,'')

			set @jl_number=@out_put
			update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number where user_serial=@user_serial and dev_serial=@a3



			--select @out_put,@jl_data
			set @res=@jl_data
			return
		end
	end
end
else if(@yich=1)  --异常状态返回
begin
	set @jl_data='3000'+','+
        cast(@out_put as varchar(10))+','+
        cast(@a27 as varchar(10))+','+
        isnull(@a1,0)+','+
        isnull(cast(@user_serial as varchar(20)),0)+','+
        isnull(@a4,0)+','+
        isnull(cast(@ac_money as varchar(20)),0)+','+
        isnull(cast(@ac_stmoney as varchar(20)),0)+','+
        isnull(cast(@ac_subo as varchar(20)),0)+','+
        isnull(cast(@ac_stsubo as varchar(20)),0)+','+

        isnull(cast(@ac_each as varchar(20)),0)+','+

        isnull(cast(@ac_steach as varchar(20)),0)

	set @jl_number=@out_put
	update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number where user_serial=@user_serial and dev_serial=@a3

			if(@out_put in (13,18,21,22,19,20,23,17,16,12,14,15,10,8,9,11)) and (@dev_type<>1)
			begin
--select @current_date,@time_rq
			if(convert(varchar(20),@current_date,23)=convert(varchar(20),@time_rq,23)) --当前日期与记录日期（偏移后）同
			begin


				select @current_bh=a.bh  --当前时段编号
				from xf_time a           --消费时段表
				inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
				where @shiduan>=a.kssj and @shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0

				if (@current_bh = null)
				begin
					set @current_bh = '0000000000000000'
				end
				if (@current_bh = @shiduan_bh) --时段同
				begin
					if(@mould=0)  --金额
					begin
						update xf_user_time set
								day_count=day_count-1,
								day_money=day_money-@aa7,
								xf_count=xf_count-1,
								xf_money=xf_money-@aa7
						where user_serial=@user_serial
					end
					else if(@mould=1) --份
					begin
						update xf_user_time set
							day_count=day_count-isnull(@aa21,0),
							xf_count=xf_count-isnull(@aa21,0)
						where user_serial=@user_serial
					end
				end
				else --时段不同
				begin
					if(@mould=0) --金额
					begin
						update xf_user_time set
							day_count=day_count-1,
							day_money=day_money-@aa7
						where user_serial=@user_serial
					end
					else if(@mould=1)  --份
					begin
						update xf_user_time set
							day_count=day_count-isnull(@aa21,0)
						where user_serial=@user_serial
					end
				end
			end

			else
			begin

				if(@mould=0)  --金额
					begin
					--select @a2,@user_serial,@aa7
					--select * from xf_user_time
						update xf_user_time set
								day_count=day_count-1,
								day_money=day_money-@aa7,
								xf_count=xf_count-1,
								xf_money=xf_money-@aa7
						where user_serial=@user_serial
					end
					else if(@mould=1) --份
					begin
						update xf_user_time set
							day_count=day_count-isnull(@aa21,0),
							xf_count=xf_count-isnull(@aa21,0)
						where user_serial=@user_serial
					end

			end

			end


	--select @out_put,@jl_data
	set @res=@jl_data
	return
end

end


else if @cmd_bz=4100		--离线记录
begin

set @a=charindex(',',@remain_data)
set @a1=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a2=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

if @a2=83
begin
	declare @jl_serial int				--终端记录流水
	declare @dev_jlrq varchar(20)		--终端交易日期
	declare @dev_jlsj varchar(20)		--终端交易时间
	declare @card_xh int				--明细编号

	set @a=charindex(',',@remain_data)
	set @dev_serial =right('000000'+left(@remain_data,@a-1),7)	--设备编号

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @jl_serial=left(@remain_data,@a-1)						--终端记录流水

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @user_serial=left(@remain_data,@a-1)					--人员序号

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @dev_jlrq=left(@remain_data,@a-1)						--交易日期

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @dev_jlsj=left(@remain_data,@a-1)						--交易时间

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)
	set @card_xh = cast(left(@remain_data,@a-1) as int)			--明细编号

	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @a=charindex(',',@remain_data)

	declare @menu_name varchar(50)
	declare @menu_bh varchar(20)
	declare @menu_money money
	declare @key_no varchar(10)
	declare @menu_count int
	declare @j int
	declare @k int
	declare @parent_bh nvarchar(100)

	set @jl_sj=cast(substring(@dev_jlrq,1,4)+'-'+substring(@dev_jlrq,5,2)
	+'-'+substring(@dev_jlrq,7,2)+' '+substring(@dev_jlsj,1,2)
	+':'+substring(@dev_jlsj,3,2)+':'+substring(@dev_jlsj,5,2) as datetime)

	if RIGHT(@remain_data,1)<>','
	begin
		set @remain_data=@remain_data+','
	end

	create table #t_cp
	(
	key_no varchar(20),
	parent_bh nvarchar(100),
	menu_name nvarchar(100),
	dev_serial varchar(10),
	more_money money,
	new_money money,
	money_count int,
	user_serial int,
	jl_sj datetime,
	sj datetime
	)

	set @a=CHARINDEX(',',@remain_data)

	while(@a<>0)	--循环处理菜谱
	begin
		begin
			set @k=CHARINDEX('|',@remain_data)
			set @j=CHARINDEX(',',@remain_data)
			set @menu_bh=SUBSTRING(@remain_data,1,@k-1)
			set @menu_count=cast(SUBSTRING(@remain_data,@k+1,@j-@k-1) as int)

			select @menu_name=menu_name from xf_menu where bh=@menu_bh and lx=2;
			select @menu_money=menu_money,@key_no=key_no,@parent_bh=parent_bh from xf_menu where bh=@menu_bh and lx=1;
			set @menu_money=@menu_money*@menu_count
			insert into #t_cp(key_no,parent_bh,menu_name,dev_serial,more_money,new_money,money_count,user_serial,jl_sj,sj)
			values(@key_no,@parent_bh,@menu_name,@dev_serial,@menu_money,@menu_money,@menu_count,@user_serial,@jl_sj,getdate())

			set @remain_data=SUBSTRING(@remain_data,@j+1,len(@remain_data)-@j)
			set @a=CHARINDEX(',',@remain_data)
		end

	end

	begin transaction;

	insert into xf_menu_jl(key_no,parent_bh,menu_name,dev_serial,jl_serial,more_money,new_money,money_count,user_serial,jl_sj,sj,card_hao)
	select key_no,parent_bh,menu_name,dev_serial,@jl_serial,more_money,new_money,money_count,user_serial,jl_sj,sj,@card_hao from #t_cp;
	drop table #t_cp;

	commit transaction;

end
else
begin
set @a=charindex(',',@remain_data)
set @a3=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a4=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a5=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a6=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a7=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a8=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a9=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a10=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a11=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a12=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a13=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a14=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a15=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a16=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a17=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a18=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a19=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a20=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a21=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a22=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a23=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a24=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a25=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a26=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a27=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
if(@a=0)
begin
set @a28=@remain_data
set @a29=0
end
else
begin
set @a28=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a29=@remain_data
end
-----------变量定义---------




--declare @user_jl_bh varchar(16)	   --记录流水号当前值，存设备变动表中
------------------------------------------------------变量赋值
--declare @auto_kssj datetime	    --自动补贴的有效开始时间
--declare @auto_jssj datetime	    --自动补贴的有效结束时间
set @user_card=@a1   --原始物理卡号
set @card_hao=right('0000000000000000'+@a1,16)      --转换为16位卡号,THEX格式
set @auto_kssj=null
set @auto_jssj=null
set @yich=0
set @current_date=getdate() --当前日期时间
set @aa7 =cast(@a18 as int)  --交易金额
set @aa21=cast(@a21 as int) --交易份数
set @out_put3 = 0
set @now_bh = 1
set @time_rq=null
---------------------------------
select @auto_kssj=sub_kssj,@auto_jssj=sub_jssj from dt_ac_link with(nolock) where user_serial=@user_serial
---------记录日期处理------------
set @sj=cast(substring(@a8,1,4)+'-'+substring(@a8,5,2)   --记录日期时间
	+'-'+substring(@a8,7,2)+' '+substring(@a9,1,2)
	+':'+substring(@a9,3,2)+':'+substring(@a9,5,2) as datetime)

set @mx_rq=cast(substring(@a8,1,4)+'-'+substring(@a8,5,2)  --明细日期
	+'-'+substring(@a8,7,2) as datetime)

set @jl_shiduan=substring(@a9,1,2)*3600+substring(@a9,3,2)*60+substring(@a9,5,2)	--记录时间对应秒数
------------------------------------------------------当前日期处理
set @current_rq=CONVERT(varchar(100), @current_date, 112)
set @current_sjd=CONVERT(varchar(100), @current_date, 24)
set @current_sjd=substring(@current_sjd,1,2)+substring(@current_sjd,4,2)+substring(@current_sjd,7,2)
set @current_shiduan=substring(@current_sjd,1,2)*3600+substring(@current_sjd,3,2)*60+substring(@current_sjd,5,2) --转换成秒数
-------------取基础数据------------
if(@a2=51)	        --区分消费模式
begin
	set @mould=0	--现金模式
end
else if(@a2=52)
begin
	set @mould=1	--份模式
end
else	            --异常数据
begin
	set @yich=1
	set @out_put=0
end
---------------

if(cast(@dev_serial as int) != cast(@a3 as int) and (@dev_lb=99)) --判断设备编号是否一致
begin
	set @yich=1
	set @out_put=0
end
else
begin
    select @dev_type=dev_type, @pay_type=isnull(pay_type,0) from st_device where bh=@dev_serial --消费机类型 0：正常 1：小卖部
end

begin transaction
SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
if(@yich!=1 and @a27=113) --指纹方式
begin
    select
        @user_serial=a.user_serial,	--人员序号
		@user_no=a2.user_no,		--人员编号
		@user_lname=a2.user_lname,	--人员姓名
		@ac_kssj = c.ac_kssj,
		@ac_jssj = c.ac_jssj,
        @card_serial=a.card_serial,     --卡顺序号
		@discount_rate=d.discount_rate, --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
		@ac_subm=isnull(a.ac_subm,0),   --补贴累计金额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
		@ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=a.ac_state,
		@ac_state1 = c.ac_state,		--账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=d.ac_time_day,     --日补贴金额
		@ac_time_offset=isnull(d.ac_time_offset,0), --时段偏移量
		@sub_order=a.sub_order,          --补贴版本号
		--@card_type=b.card_type,          --卡状态：0：正常，1：挂失

		@ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
		@ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
		@ac_sub_unit=d.ac_sub_unit,       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒
		@ac_subsidy=d.ac_subsidy
        from dt_ac_link a with(nolock)
        inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表，取出折数
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where a.user_serial=@a5     --人员序号
end
else  if (@yich!=1)  --卡消费
begin
    select @user_serial=a.user_serial,  --人员序号
		@user_no=a2.user_no,
		@user_lname=a2.user_lname,
		@ac_kssj = c.ac_kssj,
		@ac_jssj = c.ac_jssj,
        @card_serial=b.card_serial,     --卡顺序号
		@discount_rate=d.discount_rate, --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
		@ac_subm=isnull(a.ac_subm,0),   --补贴累计金额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_state=d.ac_time_state, --账户类型是否允许非时段消费
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
		@ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=a.ac_state,
		@ac_state1 = c.ac_state,        --账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=isnull(d.ac_time_day,0),       --日补贴金额
		@ac_time_offset=isnull(d.ac_time_offset,0), --时段偏移量
		@sub_order=a.sub_order,         --补贴版本号
		@card_type=b.card_type,          --卡状态：0：正常，1：挂失
		@ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
		@ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
		@ac_sub_unit=d.ac_sub_unit       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒
        from dt_ac_link a with(nolock)
        inner join dt_card b on a.user_serial=b.user_serial   --卡号表
        inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where right('0000000000000000'+b.card_hao,16)=@card_hao
end

if(@user_serial is null)    --卡或人员不存在
begin
	set @yich=1     --返回异常信息         生成系统异常
	set @out_put=0  --无此卡号
end

if(@yich!=1)
begin
    --账户未启用 --在此为账户注销 账户状态：0：启用，1：停用
    --@ac_kssj datetime           --人员账户开始时间
    --@ac_jssj datetime           --人员账户结束时间
	if (@ac_state = 1) or (@ac_state1 = 1)
    begin
        set @yich=1
        set @out_put=0 --账户停用
    end
    else if(convert(varchar(20),@ac_jssj,112) < convert(varchar(20),@current_date,112) or (convert(varchar(20),@ac_kssj,112)) > (convert(varchar(20),@current_date,112)))
    begin
        set @yich=1
        set @out_put=0 --人员账户不在有效期
    end
end

if(@yich!=1)
begin
	-----当前时段偏移量-----
	set @current_shiduan=@current_shiduan-@ac_time_offset
	if(@current_shiduan<0)
	begin
		set @current_shiduan=86400+@current_shiduan
		set @current_sj=dateadd(day,-1,@current_date)
	end
	else if(@current_shiduan>86400)
	begin
		set @current_shiduan=@current_shiduan-86400
		set @current_sj=dateadd(day,1,@current_date)
	end
	else
	begin
		set @current_sj=@current_date
	end
	-----记录时段偏移量
	set @jl_shiduan=@jl_shiduan-@ac_time_offset

	if(@jl_shiduan<0)
	begin
		set @jl_shiduan=86400+@jl_shiduan
		set @rq_sj=dateadd(day,-1,@sj)
	end
	else if(@jl_shiduan>86400)
	begin
		set @jl_shiduan=@jl_shiduan-86400
		set @rq_sj=dateadd(day,1,@sj)
	end
	else
	begin
		set @rq_sj=@sj
	end

	select @current_bh=a.bh  --当前时段编号
	from xf_time a           --消费时段表
	inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
	where @current_shiduan>=a.kssj and @current_shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0

	--记录时段编号
	select @shiduan_bh=a.bh,@discount_rate2=b.discount_rate,@shiduan_xh=b.day_order,@shiduan_ks=a.kssj,
		@shiduan_js=a.jssj,@time_limit=b.time_limit,@time_max_m=b.time_max_m,@time_max_t=b.time_max_t,@time_sub=isnull(b.time_sub,0)
	from xf_time a   --消费时段表
	inner join xf_ac_time b on b.time_no=a.bh   --账户类型对应的时段表
	where @jl_shiduan>=a.kssj and @jl_shiduan<a.jssj and b.ac_type=@ac_type and b.lx=0

	select @time_order=b.time_no,@each_unit=isnull(b.each_unit,0) from xf_time a   --消费时段表
	inner join xf_ac_time b on b.time_no=a.bh   --设备对应的时段表
	inner join dt_dev c on c.acdep_serial=b.acdep_serial
	where @jl_shiduan>=a.kssj and @jl_shiduan<a.jssj and c.dev_serial=@dev_serial and b.lx=2

	if (@mould=0 ) --金额模式
	begin
		if(@shiduan_bh is not null)  --时段内的打折
		begin
			set @discount_rate=@discount_rate2
		end
--
		set @rate=convert(money,@discount_rate)
		if(@aa7>0)
		begin
		set @new_money=@aa7*(@rate/100)      --打折后金额;终端处理
		end
		else
		begin
		set @new_money=@aa7
		end
	end
	else	--份模式，现金交易金额设为0
	begin
		set @new_money = 0
	end

	------查询设备表，判断是否为重复记录--------
	select @state_flag=state, @card_hao_dev=user_card, @a2_dev=lx, @a3_dev=dev_serial,@a4_dev=jl_bh,
		@a5_dev=user_serial,@aa7_dev=new_money,@aa21_dev=each_new,@a27_dev=ident_type,@a29_dev=ac_pass,
		@jl_number=jl_number,@jl_data =jl_data,@now_bh=now_bh
	from xf_jl_yc where dev_serial=@a3  --设备请求状态表

	if (@now_bh = null)
	begin
		set @now_bh = 1
	end

	if(@state_flag is null)	--不存在设备对应的记录
	begin
	--jl_sj            @sj                                       jl_sj=@sj
	--dev_serial       @a3               @a3_dev=dev_serial      dev_serial=@a3
	--jl_bh            @a4               @a4_dev=jl_bh,          jl_bh=@a4
	--user_serial      @user_serial      @a5_dev=user_serial     user_serial=@user_serial
	--user_card        @a1               @card_hao_dev=user_card user_card=@a1
	--lx               @a2               @a2_dev=lx              lx=@a2
	--state            0                 @state_flag=state       state=0
	--new_money        cast(@a18 as int) @aa7_dev=new_money      new_money=@aa7 --
	--each_new         @a21              @aa21_dev=each_new      each_new=@a21
	--ident_type       @a27              @a27_dev=ident_type     ident_type=@a27
	--ac_pass          isnull(@a29,0)    @a29_dev=ac_pass        ac_pass=isnull(@a29,0) 0
	--                                   @jl_number=jl_number    返回码
	--                                   @jl_data =jl_data
	--                                                           mould=@mould
	--                                                           del_type=@a15
	--                                                           type=4
	--                                                           流水号需更新为最大值

		insert into xf_jl_yc(jl_sj,dev_serial,jl_bh,user_serial,user_card,lx,state,new_money,each_new,ident_type,ac_pass,now_bh)
		--values(@sj,@a3,@a4,@user_serial,@a1,@a2,0,cast(@a18 as int),@a21,@a27,isnull(@a29,0),@now_bh)
		values(@sj,@a3,@a4,0,@a1,@a2,0,cast(@a18 as int),@a21,@a27,isnull(@a29,0),@now_bh)
	end
	else if(@state_flag=0)	--存在该设备记录
	begin
		--商户的设备
	-------判断是否为重复上传的记录,为重复记录时，直接返回-----------
		if (@user_serial = @a5_dev and @a2 = @a2_dev and @a4 = @a4_dev and cast(@a18 as int) = @aa7_dev and @aa21 = @aa21_dev
			and @a27 = @a27_dev)
			begin
				set @yich=2
				set @change_ok=1
				set @reply_ok=1
			end
		else	--不是重复记录时，更新设备表
		begin
			update xf_jl_yc --增加返回码和流水号
			set mould=@mould,type=4,
			del_type=@a15,dev_serial=@a3,jl_bh=@a4,user_serial=@user_serial,user_card=@a1,lx=@a2,state=0,
			new_money=cast(@a18 as int),each_new=@a21,ident_type=@a27,ac_pass=isnull(@a29,0),jl_sj=@sj,
			jl_data=null
			where  dev_serial=@a3
		end
	end
	else	--不存在该条记录
	begin
	 insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,state_flag,new_money,new_each,ident_type,pass,make_lx,t_level,style_yc)
	 values(@a3_dev,@a4_dev,@a5_dev,@card_hao_dev,@a2_dev,@state_flag,@aa7_dev,@aa21_dev,@a27_dev,@a29_dev,4,4,4)
	end
end

if(@yich=0)	--为正常的未处理记账记录
begin
	set @change_ok=1  --返回成功码
	set @out_put=0
end
-----------扣联机账户，人员总账户--------------
if((@yich=0) or (@yich=2))
begin
	if(@change_ok=1)								  --返回成功
	begin
		if(@reply_ok=1)	 --重复上传
		begin
			--select @jl_number,@jl_data
			set @res=@jl_data
			--return
		end
		else
		begin
			---------处理累计，补贴------------
			select @time_rq=rq,@user_time_no=time_no,@daysub_flag=sub_day, @hoursub_flag=sub_hour,
				@day_money=day_money,@day_count=day_count,@xf_money=xf_money,@xf_count=xf_count
			from xf_user_time  --累计表
			where user_serial=@user_serial
			if (@time_rq is null or @time_rq='')
			begin
				insert into xf_user_time(user_serial,day_count,day_money,xf_count,xf_money,
					time_no,rq,sub_day,sub_hour)
				values(@user_serial,0,0,0,0,
					isnull(@current_bh,'0000000000000000'),convert(varchar(10),@current_sj,23),0,0)

				set @time_rq=convert(varchar(10),@current_sj,23)
				set @user_time_no=isnull(@current_bh,'0000000000000000')
				set @hoursub_flag=0
				set @daysub_flag=0
				set	@day_count=0
				set	@day_money=0
				set	@xf_count=0
				set	@xf_money=0
			end

			---1.当前日期与记录日期一致
			-----更新累计表日期为当前日期与时段（置标志位）
			if(convert(varchar(20),@current_sj,23)!=convert(varchar(20),@time_rq,23))
			begin	--当前日期与累计表不一致，更新累计表日期时段
				update xf_user_time set
						day_count=0,
						day_money=0,
						xf_count=0,
						xf_money=0,
						time_no=isnull(@current_bh,'0000000000000000'),
						rq=convert(varchar(10),@current_sj,23),
						sub_day=0,
						sub_hour=0
				where user_serial=@user_serial

				set @time_rq=convert(varchar(10),@current_sj,23)
				set @user_time_no=isnull(@current_bh,'0000000000000000')
				set @hoursub_flag=0
				set @daysub_flag=0
				set	@day_count=0
				set	@day_money=0
				set	@xf_count=0
				set	@xf_money=0

			end
			else if(convert(varchar(20),@current_sj,23)=convert(varchar(20),@time_rq,23) and isnull(@current_bh,'0000000000000000')!=@user_time_no)
			begin	--当前日期与累计表一致，时段不一致，更新累计表时段信息
				update  xf_user_time set
						xf_count=0,
						xf_money=0,
						time_no=isnull(@current_bh,'0000000000000000'),
						sub_hour=0
				where user_serial=@user_serial
				set @user_time_no=isnull(@current_bh,'0000000000000000')
				set @hoursub_flag=0
				set	@xf_count=0
				set	@xf_money=0
			end

			--1.处理手工发放的补贴（份、现金补贴）
			--exec Pinsert_yz_sub @dev_serial,@user_serial,@out_put3     --已处理有效期，累计，周期等方式。
			if(@out_put3=1) --补贴或补贴清零，重新取账户金额
			begin
				select --根据手动补贴的返回值进行处理 也可直接计算，不进行查询
					@ac_money=isnull(a.ac_money,0),           --账户总余额
					@ac_addo=isnull(a.ac_addo,0),             --账户现金余额
					@ac_subo=isnull(a.ac_subo,0),             --账户补贴余额
					@ac_each=isnull(a.ac_eacho,0),            --份数余额
					@jl_count=a.jl_count+1,
					@sub_order=a.sub_order
				from dt_ac_link a with(nolock) where a.user_serial=@user_serial
			end

			-------以当前日期为准，与记录日期进行比较--------------
			--1.当前日期与记录日期相等需处理，日期不相等不做处理
			if(convert(varchar(20),@current_sj,23)=convert(varchar(20),@rq_sj,23) and @dev_type = 0)  --非小卖部)
			begin
				if (isnull(@current_bh,'0000000000000000')=isnull(@shiduan_bh,'0000000000000000'))
				begin
					if(@mould=0)
					begin
					update xf_user_time set
							day_count=day_count+1,
							day_money=day_money+@new_money,
							xf_count=xf_count+1,
							xf_money=xf_money+@new_money
					where user_serial=@user_serial
					end
					else if(@mould=1)
					begin
						update xf_user_time set
							day_count=day_count+isnull(@aa21,0),
							xf_count=xf_count+isnull(@aa21,0)
						where user_serial=@user_serial
					end
				end
				else --不用判断该时段是否启用限额限次，只是累计
				begin
					if(@mould=0)
					begin
					update xf_user_time set
							day_count=day_count+1,
							day_money=day_money+@new_money
					where user_serial=@user_serial
					end
					else if(@mould=1)
					begin
						update xf_user_time set
							day_count=day_count+isnull(@aa21,0)
						where user_serial=@user_serial
					end
				end
				--日补贴处理
				if(@ac_subsidy=1 and @a2 = 51 and @daysub_flag=0 and @ac_sub_auto=1 and @ac_time_type=0)	--需处理日补贴，允许自动补贴，日补方式
				begin
					if(@ac_sub_rule=1)		--周期补贴
					begin
						if(@ac_sub_limit=0) --发放后立即失效 只能吃一餐
						begin
							set @auto_kssj=dateadd(day,-1,@current_sj)
							set @auto_jssj=dateadd(day,-1,@current_sj)
						end
						else
						begin
							set @auto_kssj=@current_sj
							if(@ac_sub_unit=0) --年
							begin
								set @auto_jssj=dateadd(year,@ac_sub_limit,@current_sj)
							end
							else if(@ac_sub_unit=1) --月
							begin
								set @auto_jssj=dateadd(month,@ac_sub_limit-1,@current_date)
								select @days=(32-DAY(@auto_jssj+32-DAY(@auto_jssj))-cast(right(CONVERT(varchar(100), @current_date, 23),2) as int))
								set @auto_jssj=dateadd(day,@days,@auto_jssj)
							end
							else if(@ac_sub_unit=2) --日
							begin
								set @auto_jssj=dateadd(day,@ac_sub_limit-1,@current_sj)
							end
						end
					end
					else --累计方式
					begin
						set @auto_kssj=@current_sj
						set @auto_jssj=null
					end
					-- mould           = 0
					-- lx              = 34
					-- jl_bh           = '0000000000'
					-- jl_sj           = @current_date
					-- sam_serial      = 0
					-- old_money       = @ac_money
					-- new_money       = @ac_time_day   交易金额
					-- jl_count        = @jl_count
					-- dev_serial      = @dev_serial    --
					-- user_serial     = @user_serial
					-- dep_serial      = @dep_serial
					-- card_serial     = @card_serial
					-- sj              = @current_date
					-- user_card       = @card_hao
					-- sub_xh          = @sub_order+1   --
					-- state           = 0
					-- type            = 4       联机
					-- time_order      = '0000000000000000'  设备时段编号
					-- time_no         = '0000000000000000'  个人时段编号
					-- del_type        = 0
					-- sub_old         = @ac_subo  交易前补贴余额
					-- sub_new         = @ac_time_day
					-- each_old        = @ac_each    --
					-- each_new        = 0
					-- each_unit       = 0
					-- more_money      = 0     应收金额
					-- discount_rate   = 100
					-- gly_no          = null
					insert into xf_jl       --生成日补贴交易记录
						(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,
						card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,sub_old,sub_new,each_old,
						each_new,each_unit,more_money,discount_rate,gly_no)
					values(0,34,'0000000000',@current_date,0,@ac_money,@ac_time_day,@jl_count,@dev_serial,@user_serial,@dep_serial,
						@card_serial,@current_date,@card_hao,@sub_order+1 ,0,4,'0000000000000000','0000000000000000',0,@ac_subo,@ac_time_day,
						@ac_each,0,0,0,100,null)

					--mould           = 0
					--lx              = 34
					--type            = 1     联机
					--jl_bh           = '00000000'
					--dev_serial      = @dev_serial    --
					--user_serial     = @user_serial
					--dep_serial      = @dep_serial
					--user_card       = @card_hao
					--card_serial     = @card_serial
					--rq              = convert(varchar(20),@current_date,23)  --
					--jl_sj           = @current_date
					--old_money       = @ac_money
					--more_money      = 0
					--new_money       = 0      金额消费的消费金额
					--rate_money      = 0      优惠金额
					--discount_rate   = 100
					--money_count     = 0      消费时置1
					--new_sub         = @ac_time_day
					--sub_count       = 1      补贴时置1

					--new_add         = 0      充值金额
					--add_count       = 0      充值时置1
					--new_del         = 0      提现金额
					--del_count       = 0      提现标志
					--new_zero        = 0      补贴清零金额
					--del_zero        = 0      补贴清零标志
					--new_edit        = 0      冲正金额
					--del_edit        = 0      冲正标志
					--new_each        = 0      消费份数
					--each_unit       = 0      份单价
					--save_money      = @ac_money+@ac_time_day      交易后余额
					--jl_count        = @jl_count
					--dev_time_no     = '0000000000000000'
					--time_no         = '0000000000000000'
					--gly_no          = null
					--sj              = @current_date
					insert into xf_mx       --生成明细记录 未反映补贴版本号 当有时钟偏移时未反映日补贴的日期
						(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
						old_money,more_money,new_money,rate_money,discount_rate,money_count,
						new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
						new_each,each_unit,save_money,jl_count,dev_time_no,time_no,gly_no,sj)
					values(0,34,1,'00000000',@dev_serial,@user_serial,@dep_serial,@card_hao,@card_serial,convert(varchar(20),@current_date,23),
						@current_date,
						@ac_money,0,0,0,100,0,
						@ac_time_day,1,0,0,0,0,0,0,0,0,0,0,
						@ac_money+@ac_time_day,@jl_count,'0000000000000000','0000000000000000',null,@current_date)

					update dt_Ac_user set   --更新人员总账户
						ac_money=ac_money+isnull(@ac_time_day,0),
						ac_subm=ac_subm+isnull(@ac_time_day,0),
						ac_subo=ac_subo+isnull(@ac_time_day,0)
						where user_serial=@user_serial

					--update dt_ac_link set   --更新联机账户
					--	ac_money=ac_money+isnull(@ac_time_day,0),
					--	ac_subm=ac_subm+isnull(@ac_time_day,0),
					--	ac_subo=ac_subo+isnull(@ac_time_day,0),
					--	jl_count=@jl_count,
					--	sub_order=@sub_order+1,
					--	sub_kssj=@auto_kssj,
					--	sub_jssj=@auto_jssj
					--where user_serial=@user_serial

					set @ac_money=@ac_money+isnull(@ac_time_day,0)
					set @ac_subm=@ac_subm+isnull(@ac_time_day,0)
					set @ac_subo=@ac_subo+isnull(@ac_time_day,0)
					set @jl_count=@jl_count+1
					set @sub_order=@sub_order+1
					--@sub_kssj=@auto_kssj,
					--@sub_jssj=@auto_jssj

					--@jl_count = @jl_count + 1

					update xf_user_time set sub_day=1 where user_serial=@user_serial --更新人员交易累计表的日补贴状态
				end
				else if(@ac_subsidy=1 and @a2 = 51 and @hoursub_flag=0 and @ac_sub_auto=1 and @ac_time_type=1) and (isnull(@current_bh,'0000000000000000')=isnull(@shiduan_bh,'0000000000000000'))	--时段补贴
				begin
					if (@ac_time_mould = 0)  --时段定额补贴
					begin
						set @meal_sub = @time_sub
					end
					else if (@ac_time_mould = 1) --消费多少补多少，最大为时段定额
					begin
						if (@new_money <= @time_sub)
						begin
							set @meal_sub = @new_money
						end
						else
						begin
							set @meal_sub = @time_sub
						end
					end

					if(@ac_sub_rule=1)		--周期补贴
					begin
						if(@ac_sub_limit=0)
						begin
							set @auto_kssj=dateadd(day,-1,@current_sj)
							set @auto_jssj=dateadd(day,-1,@current_sj)
						end
						else
						begin
							set @auto_kssj=@current_sj
							if(@ac_sub_unit=0) --年
							begin
								set @auto_jssj=dateadd(year,@ac_sub_limit,@current_sj)
							end
							else if(@ac_sub_unit=1) --月
							begin
								set @auto_jssj=dateadd(month,@ac_sub_limit-1,@current_date)
								select @days=(32-DAY(@auto_jssj+32-DAY(@auto_jssj))-cast(right(CONVERT(varchar(100), @current_date, 23),2) as int))
								set @auto_jssj=dateadd(day,@days,@auto_jssj)
							end
							else if(@ac_sub_unit=2) --日
							begin
								set @auto_jssj=dateadd(day,@ac_sub_limit-1,@current_sj)
							end
						end
					end
					--mould           = 0
					--lx              = 35 --
					--jl_bh           = '0000000000'
					--jl_sj           = @current_date
					--sam_serial      = 0
					--old_money       = @ac_money
					--new_money       = @meal_sub
					--jl_count        = @jl_count
					--dev_serial      = @dev_serial    --
					--user_serial     = @user_serial
					--dep_serial      = @dep_serial
					--card_serial     = @card_serial
					--sj              = @current_date
					--user_card       = @card_hao
					--sub_xh          = @sub_order+1       --
					--state           = 0
					--type            = 4
					--time_order      = '0000000000000000'
					--time_no         = '0000000000000000'
					--del_type        = 0
					--sub_old         = @ac_subo
					--sub_new         = @meal_sub
					--each_old        = @ac_each --
					--each_new        = 0
					--each_unit       = 0
					--more_money      = 0
					--discount_rate   = 100
					--gly_no          = null
					insert into xf_jl       --生成日补贴交易记录
						(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,
						card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,sub_old,sub_new,each_old,
						each_new,each_unit,more_money,discount_rate,gly_no)
					values(0,35,'0000000000',@current_date,0,@ac_money,@meal_sub,@jl_count,@dev_serial,@user_serial,@dep_serial,
						@card_serial,@current_date,@card_hao,@sub_order+1 ,0,4,'0000000000000000','0000000000000000',0,@ac_subo,@meal_sub,
						@ac_each,0,0,0,100,null)
					--mould             = 0
					--lx                = 35  --
					--type              = 1
					--jl_bh             = '00000000'
					--dev_serial        = @dev_serial --
					--user_serial       = @user_serial
					--dep_serial        = @dep_serial
					--user_card         = @card_hao
					--card_serial       = @card_serial
					--rq                = convert(varchar(20),@current_date,23)
					--jl_sj             = @current_date
					--old_money         = @ac_money
					--more_money        = 0
					--new_money         = 0
					--rate_money        = 0
					--discount_rate     = 100
					--money_count       = 0
					--new_sub           = @meal_sub
					--sub_count         = 1
					--new_add           = 0
					--add_count         = 0
					--new_del           = 0
					--del_count         = 0
					--new_zero          = 0
					--del_zero          = 0
					--new_edit          = 0
					--del_edit          = 0
					--new_each          = 0
					--each_unit         = 0
					--save_money        = @ac_money+@meal_sub
					--jl_count          = @jl_count
					--dev_time_no       = '0000000000000000'
					--time_no           = '0000000000000000'
					--gly_no            = null
					--sj                = @current_date
					insert into xf_mx       --生成明细记录
						(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
						old_money,more_money,new_money,rate_money,discount_rate,money_count,
						new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
						new_each,each_unit,save_money,jl_count,dev_time_no,time_no,gly_no,sj)
					values(0,35,1,'00000000',@dev_serial,@user_serial,@dep_serial,@card_hao,@card_serial,convert(varchar(20),@current_date,23),
						@current_date,
						@ac_money,0,0,0,100,0,
						@meal_sub,1,0,0,0,0,0,0,0,0,0,0,
						@ac_money+@meal_sub,@jl_count,'0000000000000000','0000000000000000',null,@current_date)

					update dt_Ac_user set   --更新人员总账户
						ac_money=ac_money+isnull(@meal_sub,0),
						ac_subm=ac_subm+isnull(@meal_sub,0),
						ac_subo=ac_subo+isnull(@meal_sub,0)
						where user_serial=@user_serial

					--update dt_ac_link set   --更新联机账户
					--	ac_money=ac_money+isnull(@meal_sub,0),
					--	ac_subm=ac_subm+isnull(@meal_sub,0),
					--	ac_subo=ac_subo+isnull(@meal_sub,0),
					--	jl_count=@jl_count,
					--	sub_order=@sub_order+1,
					--	sub_kssj=@auto_kssj,
					--	sub_jssj=@auto_jssj
					--where user_serial=@user_serial

					set @ac_money=@ac_money+isnull(@meal_sub,0)
					set @ac_subm=@ac_subm+isnull(@meal_sub,0)
					set @ac_subo=@ac_subo+isnull(@meal_sub,0)
					set @jl_count=@jl_count+1
					set @sub_order=@sub_order+1
					--@sub_kssj=@auto_kssj,
					--@sub_jssj=@auto_jssj

					--@jl_count = @jl_count + 1

					update xf_user_time set sub_hour=1 where user_serial=@user_serial --更新人员交易累计表的日补贴状态
				end
			end
			------------记账记录处理，将记录插入消费记录表、消费明细表-------

			---------扣款类型 处理金额-------------
			--获取上一条记录的
			if(@new_money<0)
			begin
				select @sub_money=-isnull(sub_new,0),@add_money=-(isnull(new_money,0)-isnull(sub_new,0))
				from xf_jl where dev_serial=@dev_serial and jl_bh=@a4-1
			end
			else if(@pay_type=0)	--先扣补贴后扣账户
			begin

				 if(@ac_money<@new_money) --判断是否超额,超额 扣补贴
				 begin
					set @sub_money=@new_money-@ac_addo	--所扣补贴的金额
					set @add_money=@ac_addo				--所扣的个人账户现金金额
				 end
				 else
				 begin
					if(@ac_subo<@new_money) --补贴余额小于消费金额
					begin
						set @add_money=@new_money-@ac_subo
						set @sub_money=@ac_subo
					end
					else --补贴够扣款
					begin
						set @add_money=0
						set @sub_money=@new_money
					end
				end
			end
			else if(@pay_type=1)	--只扣补贴
			begin
				set @sub_money=@new_money
				set @add_money=0
			end
			else if(@pay_type=2)	--只扣个人账户
			begin
				set @sub_money=0
				set @add_money=@new_money
			end
			else if(@pay_type=3)	--先扣个人后扣补贴
			begin
				if(@ac_money<@new_money) --判断是否超额
				begin
					set @sub_money=@ac_subo
					set @add_money=@new_money-@ac_subo
				end
				else
				begin
					if(@ac_addo<@new_money) --个人余额小于消费金额
					begin
						set @sub_money=@new_money-@ac_addo
						set @add_money=@ac_addo
					end
					else --个人够扣款
					begin
						set @sub_money=0
						set @add_money=@new_money
					end
				end
			end
			--mould             = @mould
			--lx                = @a2
			--jl_bh             = @a4
			--jl_sj             = @sj
			--sam_serial        = 0
			--old_money         = @ac_money
			--new_money         = @new_money
			--jl_count          = @jl_count
			--dev_serial        = @dev_serial
			--user_serial       = @user_serial
			--dep_serial        = @dep_serial
			--card_serial       = @card_serial
			--sj                = @current_date
			--user_card         = @card_hao
			--sub_xh            = 0
			--state             = 0
			--type              = 4
			--time_order        = isnull(@time_order,'0000000000000000')
			--time_no           = isnull(@shiduan_bh,'0000000000000000')
			--del_type          = @pay_type       --
			--sub_old           = @ac_subo
			--sub_new           = @sub_money
			--each_old          = @ac_each
			--each_new          = isnull(@aa21,0)
			--each_unit         = @each_unit
			--more_money        = @aa7
			--discount_rate     = @discount_rate
			--gly_no            = null
			-------插入原始记录------------
			insert into xf_jl(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,dep_serial,card_serial,sj,user_card,
				sub_xh,state,type,time_order,time_no,del_type,
				sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate,gly_no)
			values(@mould,@a2,@a4,@sj,0,@ac_money,@new_money,@jl_count,@dev_serial,@user_serial,@dep_serial,@card_serial,@current_date,@card_hao,
				0,0,4,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@pay_type,
				@ac_subo,@sub_money,@ac_each,isnull(@aa21,0),@each_unit,@aa7,@discount_rate,null)

			-------生成明细记录-------------
			if(@mould=0)	--现金消费
			begin
				--mould           = @mould
				--lx              = @a2
				--type            = 1
				--jl_bh           = @a4
				--dev_serial      = @dev_serial
				--user_serial     = @user_serial
				--dep_serial      = @dep_serial
				--user_card       = @card_hao
				--card_serial     = @card_serial
				--rq              = @mx_rq    --原始记录的日期
				--jl_sj           = @sj
				--old_money       = @ac_money
				--more_money      = @aa7
				--new_money       = @new_money
				--rate_money      = @aa7-@new_money
				--discount_rate   = @discount_rate
				--money_count     = 1
				--new_sub         = 0
				--sub_count       = 0
				--new_add         = 0
				--add_count       = 0
				--new_del         = 0
				--del_count       = 0
				--new_zero        = 0
				--del_zero        = 0
				--new_edit        = 0
				--del_edit        = 0
				--new_each        = 0
				--each_unit       = 0
				--save_money      = @ac_money-@new_money
				--jl_count        = @jl_count
				--dev_time_no     = isnull(@time_order,'0000000000000000')
				--time_no         = isnull(@shiduan_bh,'0000000000000000')
				--gly_no          = null
				--sj              = @current_date
				insert into xf_mx(
					mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,
					new_each,each_unit,save_money,jl_count,dev_time_no,time_no,gly_no,sj)
				values(@mould,@a2,1,@a4,@dev_serial,@user_serial,@dep_serial,@card_hao,@card_serial,@mx_rq,@sj,
					@ac_money,@aa7,@new_money,@aa7-@new_money,@discount_rate,1,
					0,0,0,0,0,0,0,0,0,0,0,0,
					@ac_money-@new_money,@jl_count,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),null,@current_date)
			end
			else
			begin
				--mould           = @mould
				--lx              = @a2
				--type            = 1
				--jl_bh           = @a4
				--dev_serial      = @dev_serial
				--user_serial     = @user_serial
				--dep_serial      = @dep_serial
				--user_card       = @card_hao
				--card_serial     = @card_serial
				--rq              = @mx_rq
				--jl_sj           = @sj
				--old_money       = @ac_each       --份余额（份数）
				--more_money      = @aa21*isnull(@each_unit,0)    --转换成金额
				--new_money       = @aa21*isnull(@each_unit,0)    --转换成金额
				--rate_money      = 0
				--discount_rate   = 100 --@discount_rate
				--money_count     = 1
				--new_sub         = 0
				--sub_count       = 0
				--new_add         = 0
				--add_count       = 0
				--new_del         = 0
				--del_count       = 0
				--new_zero        = 0
				--del_zero        = 0
				--new_edit        = 0
				--del_edit        = 0
				--new_each        = @aa21
				--each_unit       = isnull(@each_unit,0)
				--save_money      = @ac_each-@aa21
				--jl_count        = @jl_count
				--dev_time_no     = isnull(@time_order,'0000000000000000')
				--time_no         = isnull(@shiduan_bh,'0000000000000000')
				--sj              = @current_date
				insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,each_count,
					save_money,jl_count,dev_time_no,time_no,sj)
				values(@mould,@a2,1,@a4,@dev_serial,@user_serial,@dep_serial,@card_hao,@card_serial,@mx_rq,@sj,
					@ac_each,@aa21*isnull(@each_unit,0),@aa21*isnull(@each_unit,0),0,100,1,
					0,0,0,0,0,0,0,0,0,0,@aa21,isnull(@each_unit,0),1,
					@ac_each-@aa21,@jl_count,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@current_date)
			end

			if (@mould=0)
			begin
				--更新联机账户
				set @ac_money=@ac_money-isnull(@new_money,0)
				set @ac_addo=@ac_addo-isnull(@add_money,0)
				--@ac_subm=@ac_subm+isnull(@meal_sub,0),
				set @ac_subo=@ac_subo-isnull(@sub_money,0)
				--@ac_eacho=@ac_eacho-isnull(@aa21,0),
				--set @jl_count=@jl_count+1
				--@sub_order=@sub_order+1,
				--@sub_kssj=@auto_kssj,
				--@sub_jssj=@auto_jssj

				update dt_ac_link set
					ac_money=@ac_money,
					ac_addo=@ac_addo,
					ac_subm=@ac_subm,
					ac_subo=@ac_subo,
					--ac_eacho=@ac_eacho,
					jl_count=@jl_count,
					sub_order=@sub_order,
					sub_kssj=@auto_kssj,
					sub_jssj=@auto_jssj
				where user_serial=@user_serial
				--更新总账户
				update dt_ac_user set
					ac_money=ac_money-isnull(@new_money,0),
					ac_addo=ac_addo-isnull(@add_money,0),
					ac_subo=ac_subo-isnull(@sub_money,0)
					--ac_eacho=ac_eacho-isnull(@aa21,0)
				where user_serial=@user_serial
			end
			else
			begin
				--更新联机账户
				--@ac_money=@ac_money-isnull(@new_money,0),
				--@ac_addo=@ac_addo-isnull(@add_money,0),
				--@ac_subm=@ac_subm+isnull(@meal_sub,0),
				--@ac_subo=@ac_subo-isnull(@sub_money,0),
				set @ac_each=@ac_each-isnull(@aa21,0)
				--set @jl_count=@jl_count+1
				--@sub_order=@sub_order+1,
				--@sub_kssj=@auto_kssj,
				--@sub_jssj=@auto_jssj

				update dt_ac_link set
					--ac_money=@ac_money,
					--ac_addo=@ac_addo,
					--ac_subm=@ac_subm,
					--ac_subo=@ac_subo,
					ac_eacho=@ac_each,
					jl_count=@jl_count
					--sub_order=@sub_order,
					--sub_kssj=@auto_kssj,
					--sub_jssj=@auto_jssj
				where user_serial=@user_serial
				--更新总账户
				update dt_ac_user set
					--ac_money=ac_money-isnull(@new_money,0),
					--ac_addo=ac_addo-isnull(@add_money,0),
					--ac_subo=ac_subo-isnull(@sub_money,0),
					ac_eacho=ac_eacho-isnull(@aa21,0)
				where user_serial=@user_serial
			end

			--------------------------------------返回成功状态
			set @jl_data='4100'+','+
			cast(@out_put as varchar(10))+','+
			cast(@a27 as varchar(10))+','+
			cast(isnull(@user_card,'') as varchar(16))+','+
			cast(isnull(@user_serial,'') as varchar(16))+','+
			cast(isnull(@a4,'') as varchar(16))
			set @jl_number=@out_put

			--select @jl_number,@jl_data
			set @res=@jl_data
			--return


		if (@a4 > @now_bh)
		begin
			set @now_bh = @a4
		end
		update xf_jl_yc set user_serial=@user_serial,jl_data=@jl_data,jl_number=@jl_number,now_bh = @now_bh,jl_bh=@a4 where dev_serial=@a3
	end
end
end
else if(@yich=1)	--返回异常
begin
	set @jl_data='4100'+','+
		cast(@out_put as varchar(10))+','+
		cast(@a27 as varchar(10))+','+
		cast(isnull(@user_card,'') as varchar(16))+','+
		cast(isnull(@user_serial,'') as varchar(16))+','+
		cast(isnull(@a4,'') as varchar(16))
	set @jl_number=@out_put

	--select @jl_number,@jl_data
	insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,new_money,new_each,ident_type,pass,make_lx,t_level,style_yc)
	 values(@dev_serial,@a4,@a5,@card_hao,@a2,@aa7,@aa21,@a27,@a29,5,5,5)
	--update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number where dev_serial=@a3
end

end

if (@@error>0)
begin
	rollback
end
else
begin
	commit transaction
	--set @res=@jl_data	20151123 离线记录取消应答
	--return
end

end



else if @cmd_bz=5000		--冲正消费
begin


----------------------------------------------------异常状态报告
set @a=charindex(',',@remain_data)
set @style=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @card_hao=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @user_serial=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @jl_bh=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @dev_serial2=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @rq=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @sj=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @pay_type=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @new_money=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @new_each=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @each_del=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @user_password=@remain_data

------判断使用哪个设备编号---------
set @dev_serial1='001'+right('0000'+@dev_serial,4)
set @dev_serial2='001'+right('0000'+@dev_serial2,4)

if((select count(bh) from st_device where bh=@dev_serial2)=0)
begin
	set @dev_serial=@dev_serial1
end
else
begin
	set @dev_serial=@dev_serial2
end
---------------------------------记录时间
set @jl_sj=cast(substring(@rq,1,4)+'-'+substring(@rq,5,2)
+'-'+substring(@rq,7,2)+' '+substring(@sj,1,2)
+':'+substring(@sj,3,2)+':'+substring(@sj,5,2) as datetime)





----------------------------------------------------------
begin transaction
SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
set @current_date=getdate()
if(@style!=113) --卡方式
begin
    select @user_serial=a.user_serial,  --人员序号
			@jl_count=a.jl_count+1
	from dt_ac_link a with(nolock)
	inner join dt_card b on a.user_serial=b.user_serial   --卡号表
	where b.card_hao=@card_hao
end
else if(@style=113)	--指纹消费
begin
    select @user_serial=a.user_serial,  --人员序号
			@jl_count=a.jl_count+1
	from dt_ac_link a with(nolock)
	where a.user_serial=@user_serial
	--inner join dt_card b on a.user_serial=b.user_serial   --卡号表
end
-------------------------------------------------------
set @back_ok=1
set @out_put=0
if(@new_each='' or @new_each is null)
begin
	set @new_each=0
end

select @state_flag=state,@card_hao_dev=user_card, @a2_dev=lx, @a3_dev=dev_serial,@a4_dev=jl_bh,
       @a5_dev=user_serial,@aa7_dev=more_money,@aa21_dev=each_new,@a27_dev=ident_type,@a29_dev=ac_pass,@jl_data =jl_data,
	   @ac_blocks=isnull(sub_new,0),@ac_blockm=(isnull(new_money,0)-isnull(sub_new,0)),@ac_blocke=isnull(each_new,0)
from xf_jl_yc where dev_serial=cast(@dev_serial2 as int)  --设备请求状态表

if(@state_flag=1)
begin
	if (@user_serial = @a5_dev  and @jl_bh = @a4_dev and @new_money = @aa7_dev and @new_each = @aa21_dev
        and @style = @a27_dev and @user_password = @a29_dev) --正常的撤销
	begin
		insert into xf_jl
			(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,
			card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,
			sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate)
		select isnull(mould,0),1,jl_bh,@current_date,sam_serial,old_money,-new_money,@jl_count,@dev_serial,user_serial,
			card_serial,@current_date,user_card,sub_xh,0,type,time_order,time_no,del_type,
			isnull(sub_old,0),-sub_new,each_old,-each_new,isnull(each_unit,0),more_money,isnull(discount_rate,0)
		from xf_jl_yc where dev_serial=cast(@dev_Serial as int)

		insert into xf_mx
			(mould,lx,type,jl_bh,dev_serial,user_serial,user_card,card_serial,rq,jl_sj,
			old_money,more_money,new_money,discount_rate,money_count,
			new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,
			each_unit,jl_count,dev_time_no,time_no,sj)
		select isnull(mould,0),lx,type,jl_bh,@dev_serial,user_serial,user_card,card_serial,convert(varchar(20),@current_date,23),@current_date,
			old_money-new_money,-more_money,-new_money,discount_rate,1,
			0,0,0,0,0,0,0,0,0,0,-each_new,isnull(each_unit,0),
			@jl_count,time_order,time_no,@current_date
		from xf_jl_yc where dev_serial=cast(@dev_serial as int)

		update dt_ac_link set
			ac_money=ac_money+(@ac_blockm+@ac_blocks),
			ac_subo=ac_subo+@ac_blocks,
			ac_addo=ac_addo+@ac_blockm,
			--ac_blocks=ac_blocks-@ac_blocks,
			--ac_blockm=ac_blockm-@ac_blockm,
			ac_eacho=ac_eacho+@ac_blocke,
			--ac_blocke=ac_blocke-@ac_blocke,
			jl_count=jl_count+1
		where user_serial=@user_serial

		update dt_ac_user set
			ac_money=ac_money+(@ac_blockm+@ac_blocks),
			ac_subo=ac_subo+@ac_blocks,
			ac_addo=ac_addo+@ac_blockm
		where user_serial=@user_serial

		update xf_jl_yc set state=0 where dev_serial=cast(@dev_serial as int)
	end
	else
	begin	--不正常的撤销
		insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,state_flag,new_money,new_each,ident_type,pass,
			make_lx,t_level,style_yc,yc_sj)
		values(@a3_dev,@a4_dev,@a5_dev,@card_hao_dev,@a2_dev,@state_flag,@aa7_dev,@aa21_dev,@a27_dev,@a29_dev,
			2,2,2,@current_date)
	end
end

--select 0,'5000'+','+cast(@out_put as varchar(10))+','+cast(@style as varchar(10))+','+isnull(@card_hao,'')+','
--	+cast(isnull(@user_serial,'') as varchar(20))+','+isnull(@jl_bh,'')

if (@@error>0)
begin
	rollback
end
else
begin
	commit transaction
	set @res='5000'+','+cast(@out_put as varchar(10))+','+cast(@style as varchar(10))+','+isnull(@card_hao,'')+','
	+cast(isnull(@user_serial,'') as varchar(20))+','+isnull(@jl_bh,'')
	return
end


end


else if @cmd_bz=2100		--充值权限认证
begin


set @current_date=getdate()

set @a=charindex(',',@remain_data)
set @command_type=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @command_password=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @card_hao=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @user_serial=left(@remain_data,@a-1)
set @dev_bh=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @dev_bh='001'+right('0000'+@dev_bh,4)
set @card_hao2=right('0000000000000000'+@card_hao,16)
if(cast(@command_type as int)=114 or cast(@command_type as int)=113)	--指纹加密码,指纹验证
begin
		select
        @user_serial2=a.user_serial	--人员序号
		from dt_ac_link a with(nolock)
        inner join dt_ac_user c  with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where a.user_serial=@user_Serial     --人员序号
	if(@user_serial2 is null)    --人员不存在
    begin
		--select 7,'2100'+','+'7';
		set @res='2100'+','+'7';
		return
    end
end
else	--卡加密码，卡验证
begin
        if not exists(select 1 from dt_card where lx=1
        and right('0000000000000000'+card_hao,16)=@card_hao2
        )
       begin
		--select 7,'2100'+','+'7';
		set @res='2100'+','+'6';
		return
      end
	  select @cz_no=gly_no,@user_serial=user_serial
	  from dt_card where right('0000000000000000'+card_hao,16)=@card_hao2 and lx=1

	  --select @cz_name= from dt_add
end

if(@dev_bh!=@dev_serial)	--设备号异常
begin
	--select 1,'2100'+','+'1';
	set @res='2100'+','+'1';
	return
end
if((select count(*) from st_device where bh=@dev_serial)=0)
begin	--无此设备
	--select 24,'2100'+','+'24';
	set @res='2100'+','+'24';
	return
end
/*
if((select count(*) from add_gly where user_serial=@user_serial2)=0)		--充值权限人员
begin
	--select 106,'2100'+','+'106';
	set @res='2100'+','+'106';
	return
end
*/
if((select count(*) from st_device where bh=@dev_serial and dev_type=2)=0)		--充值权限设备
begin
	--select 102,'2100'+','+'102';
	set @res='2100'+','+'102';
	return
end
select @user_password=user_password,@cz_name=user_lname,@gly_kssj=pact_begin,@gly_jssj=pact_end
 from dt_addgly where user_no=@cz_no
if(convert(varchar(20),@gly_jssj,112) < convert(varchar(20),@current_date,112)
or (convert(varchar(20),@gly_kssj,112)) > (convert(varchar(20),@current_date,112)))
begin	--验证管理员卡有效期

	set @res='2100'+','+'107';
	return

end
if(@user_password!=@command_password) --密码验证错误
begin
	--select 23,'2100'+','+'23';
	set @res='2100'+','+'23';
	return
end

--更新充值设备验证时间
update st_device set add_sj=getdate() where bh=@dev_serial

--权限验证成功
--select 0,'2100'+','+'0';

set @res='2100'+','+'0'+','+cast(isnull(@user_serial,'0') as varchar(10))+','+isnull(@cz_no,'')+','+isnull(@cz_name,'');
return


end

else if @cmd_bz=2200		--注销充值权限
begin


set @a=charindex(',',@remain_data)
set @command_type=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @command_password=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @card_hao=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @user_serial=left(@remain_data,@a-1)
set @dev_bh=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @dev_bh='001'+right('0000'+@dev_bh,4)
set @card_hao2=right('0000000000000000'+@card_hao,16)
if(cast(@command_type as int)=114 or cast(@command_type as int)=113)	--指纹加密码
begin
	select
    @user_serial2=a.user_serial	--人员序号
	from dt_ac_link a with(nolock)
    inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
    inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
    where a.user_serial=@user_Serial     --人员序号
	if(@user_serial2 is null)    --人员不存在
    begin
		--select 7,'2200'+','+'7';
		set @res='2200'+','+'106'
		return
    end
end
else	--卡加密码
begin
        if not exists(select 1 from dt_card where lx=1
        and right('0000000000000000'+card_hao,16)=@card_hao2
        )
       begin
		--select 7,'2100'+','+'7';
		set @res='2100'+','+'106';
		return
      end
	  select @cz_no=gly_no from dt_card where right('0000000000000000'+card_hao,16)=@card_hao2 and lx=1
end
if(@dev_bh!=@dev_serial)	--设备号异常
begin
	--select 1,'2200'+','+'1';
	set @res='2200'+','+'1'
	return
end
if((select count(*) from st_device where bh=@dev_serial and dev_type=2)=0)
begin	--无此设备
	--select 24,'2200'+','+'24';
	set @res='2200'+','+'24'
	return
end

if((select count(*) from st_device where bh=@dev_serial and dev_type=2)=0)		--充值权限设备
begin
	--select 102,'2200'+','+'102';
	set @res='2200'+','+'102'
	return
end
--select 0,'2200'+','+'0';

update st_device set add_sj=null where bh=@dev_serial;

set @res='2200'+','+'0'
return


end


else if @cmd_bz=3100		--联机充值
begin

set @a=charindex(',',@remain_data)
set @a1=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a2=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a3=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a4=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a5=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a6=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a7=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a8=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a9=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a10=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a11=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a12=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a13=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a14=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a15=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a16=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a17=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a18=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a19=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a20=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a21=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a22=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a23=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a24=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a25=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a26=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a27=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

set @a=charindex(',',@remain_data)
set @a28=left(@remain_data,@a-1)
set @a29=substring(@remain_data,@a+1,len(@remain_data)-@a)

/*
set @a=charindex(',',@remain_data)
set @a29=left(@remain_data,@a-1)
set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)


set @a=charindex(',',@remain_data)
set @a30=left(@remain_data,@a-1)

set @a31=substring(@remain_data,@a+1,len(@remain_data)-@a)
*/
----变量定义


set @insert_yc=0
set @daysub_flag=0
set @hoursub_flag=0
set @change_ok=0
set @yich=0
set @day_money=0
set @day_count=0
set @xf_money=0
set @xf_count=0
set @xf_each=0
set @pass=0
set @reply_ok=0
set @out_put3=1
set @out_put=0
set @auto_kssj=null
set @auto_jssj=null
set @dis_online=0

---



set @dev_serial='001'+right('0000'+@dev_serial,4)
set @dev_bh='001'+right('0000'+@a3,4)
set @card_hao=right('0000000000000000'+@a1,16)
set @aa7=convert(int,@a7)      --应收金额

-----------------------------------------日期处理
set @current_date=getdate()            --统一日期时间
set @rq=CONVERT(varchar(100), @current_date, 112)
set @sj=CONVERT(varchar(100), @current_date, 24)
set @sj=substring(@sj,1,2)+substring(@sj,4,2)+substring(@sj,7,2)
set @shiduan=substring(@sj,1,2)*3600+substring(@sj,3,2)*60+substring(@sj,5,2) --转换成秒数

if((select count(bh) from st_device where bh=@dev_serial)=0)
begin	--无此设备
set @yich=1
set @out_put=24
end
---判断设备是否经过认证，设备是否停用
if(@yich!=1)
begin
select @add_sj=add_sj,@dev_lb=dev_lb from st_device where bh=@dev_bh
set @add_mi=CONVERT(varchar(100), @add_sj, 24)
set @add_mi=substring(@add_mi,1,2)+substring(@add_mi,4,2)+substring(@add_mi,7,2)
set @add_ss=substring(@add_mi,1,2)*3600+substring(@add_mi,3,2)*60+substring(@add_mi,5,2) --转换成秒数
if (@dev_serial != @dev_bh) --判断设备编号是否一致,设备未删除
begin
	set @yich=1
	set @out_put=1
end
--select @add_ss,@shiduan 跨天或超过设定的间隔时间，需要重新签到
else if((@add_sj is null)
or(convert(varchar(20),@add_sj,112)=convert(varchar(20),@current_date,112) and @shiduan-@add_ss>@sign_time)
or (convert(varchar(20),@add_sj,112)!=convert(varchar(20),@current_date,112)))
--验证认证权限
begin
	set @yich=1
	set @out_put=101
end
end
---判断人员有效性
--begin transaction
--SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
if(@yich!=1 and @a27=113) --指纹方式
begin
    select
        @user_serial=a.user_serial,	--人员序号
		@user_no=a2.user_no,		--人员编号
		@user_lname=a2.user_lname,	--人员姓名
		@user_depname=a2.user_depname,
        @card_serial=a.card_serial,     --卡顺序号
        @discount_rate=d.discount_rate, --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_state=d.ac_time_state, --账户类型是否允许非时段消费
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
		@ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=a.ac_state,           --账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=d.ac_time_day,     --日补贴金额
		@day_max_t=d.day_max_t,         --账户日限次
		@day_max_m=d.day_max_m,         --账户日限额
		@once_max_m=d.time_max_m,       --账户单次限额
		@ac_time_offset=d.ac_time_offset, --时段偏移量
		@pass_max=d.pass_max,            --密码是否启用
        @pass_word=c.ac_pass,            --密码
		@sub_order=a.sub_order,			 --当前补贴版本号
		@ac_kssj=c.ac_kssj,
		@ac_jssj=c.ac_jssj,                                 --卡状态		                                 --卡状态
		@ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
		@ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
		@ac_sub_unit=d.ac_sub_unit,       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒（目前只用年月日）
		@ac_subsidy=d.ac_subsidy
        from dt_ac_link a with(nolock)
        --inner join dt_card b on a.user_serial=b.user_serial   --卡号表， 不再需要
        inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表，取出折数
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where a.user_serial=@a5     --人员序号

    if(@user_serial is null)    --人员不存在
    begin
        set @yich=1 --返回异常信息
        set @out_put=7
    end

	--查询充值员编号 根据充值员序号@12
	if(@a11='')
	begin
		select @add_gly=a.user_no
		from add_gly a
		where a.user_serial=@a12
	end
	else
	begin
	    if not exists(select 1 from dt_card where lx=1
        and right('0000000000000000'+card_hao,16)=right('0000000000000000'+@a11,16)
        )
		begin
			set @yich=1
			set @out_put=106
			--return
		end
		else
		begin
			select @add_gly=a.user_no
			from dt_addgly a

			inner join dt_card b on a.user_no=b.gly_no
			where right('0000000000000000'+b.card_hao,16)=right('0000000000000000'+@a11,16)
		end
		end

end
else if (@yich!=1) --卡消费
begin
    select @user_serial=a.user_serial,  --人员序号
		@user_no=a2.user_no,
		@user_lname=a2.user_lname,
		@user_depname=a2.user_depname,
        @card_serial=b.card_serial,     --卡顺序号
        @discount_rate=d.discount_rate, --打折折数
        @ac_money=isnull(a.ac_money,0), --账户总余额
        @ac_addo=isnull(a.ac_addo,0),   --账户现金余额
        @ac_subo=isnull(a.ac_subo,0),   --账户补贴余额
        @ac_each=isnull(a.ac_eacho,0),  --账户份数余额
        @jl_count=a.jl_count+1,         --卡交易次数
        @ac_type=c.ac_type,             --账户类型
        @ac_time_state=d.ac_time_state, --账户类型是否允许非时段消费
        @ac_time_type=d.ac_time_type,   --自动补贴类型 0：日补贴，1：时段补贴
		@ac_time_mould=d.ac_time_mould,	--时段补贴方式
        @dep_serial=a2.user_dep,
        @ac_state=a.ac_state,           --账户状态，0:启用 1:停用
        @ac_sub_auto=d.ac_sub_auto,     --是否启用自动补贴
        @ac_time_day=isnull(d.ac_time_day,0),     --日补贴金额
		@day_max_t=d.day_max_t,         --账户日限次
		@day_max_m=d.day_max_m,         --账户日限额
		@once_max_m=d.time_max_m,       --账户单次限额
		@ac_time_offset=d.ac_time_offset,--时段偏移量
		@pass_max=d.pass_max,            --密码是否启用
		@pass_word=c.ac_pass,            --密码
		@sub_order=a.sub_order,          --当前补贴版本号
		@card_type=b.card_type,          --卡状态：0：正常，1：挂失
		@ac_kssj=c.ac_kssj,
		@ac_jssj=c.ac_jssj,                                 --卡状态
		@ac_sub_rule=d.ac_sub_rule,      --补贴规则：0：补贴累计 1：周期补贴
		@ac_sub_limit=d.ac_sub_limit,    --补贴累计周期：从补贴发放时间开始计算 0：表示无限期累计
		@ac_sub_unit=d.ac_sub_unit,       --补贴有效周期单位：0：年，1：月，2：日 3：时，4：分，5：秒
		@ac_subsidy=d.ac_subsidy
        from dt_ac_link a with(nolock)
        inner join dt_card b on a.user_serial=b.user_serial   --卡号表
        inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
        inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表
        inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
        where right('0000000000000000'+b.card_hao,16)=@card_hao
    ----------------------------------------------------------------------人员信息预判断

    if(@user_serial is null)    --卡不存在
    begin
        set @yich=1     --返回异常信息
        set @out_put=6  --无此卡号
    end
    else if(@card_type=1)
    begin
        set @yich=1
        set @out_put=5 --挂失
    end

	--查询充值员编号 根据充值员序号@12
	if(@a11='')
	begin
		select @add_gly=a.user_no
		from add_gly a
		where a.user_serial=@a12
	end
	else
	begin
	    if not exists(select 1 from dt_card where lx=1
        and right('0000000000000000'+card_hao,16)=right('0000000000000000'+@a11,16)
        )
		begin
			set @yich=1
			set @out_put=106
			--return
		end
		else
		begin
			select @add_gly=a.user_no
			from dt_addgly a
			inner join dt_card b on a.user_no=b.gly_no
			where right('0000000000000000'+b.card_hao,16)=right('0000000000000000'+@a11,16)
		end

	end

end

select @once_add_max=isnull(time_max_m,0),@day_add_count=isnull(day_max_t,0),
@day_add_max=isnull(day_max_m,0),@gly_kssj=pact_begin,@gly_jssj=pact_end
from dt_addgly where user_no=@add_gly

if(convert(varchar(20),@gly_jssj,112) < convert(varchar(20),@current_date,112)
or (convert(varchar(20),@gly_kssj,112)) > (convert(varchar(20),@current_date,112)))
begin
	set @yich=1
	set @out_put=107

end

--提现时判断提现额度
if(@yich<>1 and @aa7<0 and abs(@aa7)>@ac_addo)
begin

	set @yich=1
	set @out_put=10

end
if(@yich!=1)
begin
    --账户未启用 --在此为账户注销 账户状态：0：启用，1：停用
    --@ac_kssj datetime           --人员账户开始时间
    --@ac_jssj datetime           --人员账户结束时间

	if (@ac_state = 1)
    begin
        set @yich=1
        set @out_put=2 --账户停用
    end
    else if(convert(varchar(20),@ac_jssj,112) < convert(varchar(20),@current_date,112) or (convert(varchar(20),@ac_kssj,112)) > (convert(varchar(20),@current_date,112)))
    begin
        set @yich=1
        set @out_put=3 --人员账户不在有效期
    end
end
	if(@yich!=1)
	begin
	----判断单次限额
	/* 注释;调整成从xf_addmoney取值;xf_mx汇总数据太慢，影响正常消费过程
	select @dev_count=sum(add_count),@dev_max=sum(new_add) from xf_mx
	where convert(varchar(20),jl_sj,112)=convert(varchar(20),@current_date,112)
	and gly_no=@add_gly
	*/
	select @dev_count=count(1),@dev_max=sum(add_money) from xf_addmoney
	where convert(varchar(20),update_sj,112)=convert(varchar(20),@current_date,112)
	and gly_no=@add_gly


	if(@once_add_max!=0 and @once_add_max<@aa7)
	begin
		set @yich=1
        set @out_put=103
	end
	----判断日累计最大次数
	else if(@day_add_count!=0 and @day_add_count<isnull(@dev_count,0)+1)
	begin
			set @yich=1
			set @out_put=104
	end
	----判断日累计最大金额
	else if(@day_add_count!=0 and @day_add_max<isnull(@dev_max,0)+@aa7)
	begin
				set @yich=1
				set @out_put=105
	end
	end

------查询是否存在其他的未处理的消费请求
--冻结状态表 一台设备对应一条记录
--字段：设备编号、冻结标志（0：未冻结，1：冻结）、返回的记录内容

--物理卡号 @card_hao
--交易类型 @a2 1：金额消费、41：份消费
--设备编号 @a3
--交易流水号（记录流水号，按设备，顺序增加） @a4
--卡顺序号（在此为人员序号） @a5
--应收金额 @aa7
--交易份数 @aa21
--识别介质 @a27
--交易密码 @a29
----------------------------------------------

--20191204 lrh 新增联机充值场所白名单验证beg
declare @dep_serial3100 varchar(20)
select @dep_serial3100=acdep_serial from dt_dev where dev_serial=@dev_serial and lx=3

if(@yich!=1)
begin
		if(select count(user_serial) from dt_user where user_serial=@user_serial and user_type<>51)=0 --无此人员
		begin
			set @yich=1
			set @out_put=6
		end
		else if (select count(1) from
			(

				select  a.user_serial
				from dt_ac_dep_user a
				inner join dt_user b on a.user_serial=b.user_serial
				where a.dep_serial=@dep_serial3100 and b.user_type<51
				and isnull(module_id,'0035')='0010'
				union all
				select  a.user_serial
				from dt_user a
				inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
				inner join dt_ac_dep f on b.ac_serial=f.dep_serial
				where f.dep_serial=@dep_serial3100
				and a.user_type<51
				)a1)<>0 and (
			select count(1) from
			(

				select  a.user_serial
				from dt_ac_dep_user a
				inner join dt_user b on a.user_serial=b.user_serial
				where a.dep_serial=@dep_serial3100 and b.user_type<51
				and isnull(module_id,'0035')='0010'
				union all
				select  a.user_serial
				from dt_user a
				inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
				inner join dt_ac_dep f on b.ac_serial=f.dep_serial
				where f.dep_serial=@dep_serial3100
				and a.user_type<51
				)a1 where a1.user_serial=@user_serial
			)=0
		begin
			--增加判断无权限卡，场所未指定白名单的卡 1.取设备对应场所的白名单(带继承关系)；2.
			set @yich=1
			set @out_put=26
		end
end
--20191204 lrh 新增联机充值场所白名单验证end


if(@yich!=1)
begin
	set @state_flag = null
	select
	@state_flag=state,@card_hao_dev=user_card, @a2_dev=lx, @a3_dev=dev_serial,
	@a4_dev=jl_bh,@a5_dev=user_serial,@aa7_dev=more_money,@aa21_dev=each_new,
	@a27_dev=ident_type,@a29_dev=ac_pass,@jl_number=jl_number,@jl_data =jl_data,@sj_dev=jl_sj
	from xf_jl_yc where dev_serial=@a3

	if (@state_flag is null)
	begin
		--插入记录
		insert into xf_jl_yc(jl_sj,dev_serial,jl_bh,user_serial,user_card,lx,state,
		more_money,each_new,ident_type,ac_pass,now_bh,add_gly)
		values(@jl_sj,@a3,@a4,@user_serial,@a1,@a2,1,
		cast(@a18 as int),@a21,@a27,isnull(@a29,0),0,@add_gly)

		 --给变量赋值
		set @state_flag=1
		set @card_hao_dev=@a1
		set @a2_dev=@a2
		set @a3_dev=@a3
		set @a4_dev=@a4
		set @a5_dev=@user_serial
		set @aa7_dev=cast(@aa7 as int)
		set @aa21_dev=@a21
		set @a27_dev=@a27
		set @a29_dev=@a29

	end
	else if(@state_flag<>0)
	begin
		if(@sj_dev=@jl_sj and @user_serial = @a5_dev and @a2 = @a2_dev and @a4 = @a4_dev and @aa7 = @aa7_dev and @aa21 = @aa21_dev and @a27 = @a27_dev and @a29 != @a29_dev )
		begin
			set @yich=0
		end
		else if (@sj_dev=@jl_sj and @user_serial = @a5_dev and @a2 = @a2_dev and @a4 = @a4_dev and @aa7 = @aa7_dev and @aa21 = @aa21_dev and @a27 = @a27_dev and @a29 = @a29_dev ) --重复请求
		begin  --重复请求
			set @yich=2
			set @reply_ok=1
			set @change_ok=1
		end
		/*
		else if(@a3=@a3_dev and @a4-@a4_dev<1) --系统异常 请求消费时设备请求表有未处理的记录，替换该条记录
		begin
			 --放入异常表
			insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,state_flag,new_money,new_each,dev_type,pass,make_lx,t_level,style_yc)
			values(@a3_dev,@a4_dev,@a5_dev,@card_hao_dev,@a2_dev,@state_flag,@aa7_dev,@aa21_dev,@a27_dev,@a29_dev,1,1,1)
			set @yich=0
			set @change_ok=1
		end
		*/
	end



if((@yich=0) or (@yich=2))
begin

	--if(@change_ok=1)
	--begin
		if(@reply_ok=1)  --重复请求
		begin

			--select @jl_number,@jl_data
			update st_device set add_sj=@current_date where bh=@dev_serial;
		end
		else  --新的请求
		begin

			--set @jl_count=@jl_count+1
			-----将记录表插入临时记录表
				--select
				--    @ac_money=a.ac_money,           --账户总余额
				--    @ac_addo=a.ac_addo,             --账户现金余额
				--    @ac_subo=a.ac_subo,             --账户补贴余额
				--    @ac_each=a.ac_eacho,            --账户份数余额
				--    @jl_count=a.jl_count+1         --卡交易次数
				--from dt_ac_link a where user_serial=@user_serial
			--if(@change_ok<>1)
			--begin
			update xf_jl_yc
			set mould=@mould,type=4,discount_rate=@discount_rate,old_money=@ac_money,time_order=@time_order,time_no=@shiduan_bh,
				del_type=@a15,dev_serial=@a3,jl_bh=@a4,user_serial=@user_serial,user_card=@a1,lx=isnull(@a2,0),state=1,
				new_money=@aa7,more_money=cast(@a7 as int),each_new=@a21,ident_type=@a27,ac_pass=isnull(@a29,0),
				sub_old=@ac_subo,sub_new=@sub_money,dep_serial=@dep_serial,each_unit=isnull(@each_unit,0),
				card_serial=@card_serial,each_old=@ac_each,jl_count=@jl_count,jl_sj=@jl_sj,
				jl_data=null,now_bh=@a4,add_gly=@add_gly
			where  dev_serial=@a3


-----------------请求时生成冻结记录 lx=36 冻结 lx=37 解冻
			insert into xf_jl   --将终端读取的记录插入原始记录表
				(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,
				dep_serial,card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,
				sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate)
			values(isnull(@mould,0),2,@a4,@current_date,0,@ac_money,@aa7,@jl_count,@dev_serial,@user_serial,
				@dep_serial,@card_serial,@current_date,@card_hao,@a12,0,4,isnull(@time_order,'0000000000000000'),isnull(@shiduan_bh,'0000000000000000'),@a15,
				@ac_subo,@sub_money,@ac_each,@a21,isnull(@each_unit,0),cast(@a18 as money),@discount_rate)
-------不生成明细 记录确认后生成明细
				insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
					old_add,old_sub,old_money,more_money,new_money,rate_money,discount_rate,money_count,
					new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
					save_add,save_sub,save_money,jl_count,dev_time_no,time_no,sj,gly_no)
			   select 0,2,1,@a4,@dev_serial,@user_serial,@dep_serial,@card_hao,@card_serial,convert(varchar(20),@current_date,23),@current_date,
					@ac_addo,@ac_subo,old_money,0,0,0,discount_rate,0,
					0,0,new_money,1,0,0,0,0,0,0,each_new,isnull(each_unit,0),
					@ac_addo+new_money,@ac_subo,old_money+new_money,jl_count,isnull(time_order,'0000000000000000'),isnull(time_no,'0000000000000000'),@current_date
					,@add_gly
				from xf_jl_yc where user_serial=@user_serial and dev_serial=@a3

				insert into xf_addmoney(user_serial,input_money,add_money,gly_money,sj,update_sj,add_state,gly_no,type)
				select @user_serial,new_money,new_money,0,getdate(),getdate(),2,@add_gly,1
				from xf_jl_yc where user_serial=@user_serial and dev_serial=@a3

				  --update xf_jl_yc set state=0 where dev_serial=@a3
			-----更新联机账户，冻结金额，总账户金额不变-----
			if(@mould=1)  --份模式
			begin
				update dt_ac_link set
					ac_eacho=isnull(ac_eacho,0)-isnull(cast(@a21 as int),0),    --份余额
					ac_blocke=isnull(ac_blocke,0)+isnull(cast(@a21 as int),0),  --冻结份数
					jl_count=@jl_count                                          --卡片交易计数
				where user_serial=@user_serial
			end
			else  --金额模式
			begin
				update dt_ac_link set
					ac_money=isnull(ac_money,0)+isnull(@aa7,0),                             --联机总账户余额
					ac_addo=isnull(ac_addo,0)+(isnull(@aa7,0)-isnull(@sub_money,0)),        --个人账户余额
					ac_blockm=isnull(ac_blockm,0)+isnull(@aa7,0),    --个人账户冻结金额
					jl_count=@jl_count                                                      --卡片交易计数
				where user_serial=@user_serial

			update dt_ac_user set
			ac_money=ac_money+@aa7,
			ac_addo=ac_addo+@aa7,
			ac_addm=ac_addm+@aa7
		where user_serial=@user_serial
			end
			--end
--select @rq,@sj,@each_lx,@aa7,@a27,@a1
--select @out_put
--waitfor delay '00:00:05'
			set @jl_data='3100'+','+
				cast(@out_put as varchar(10))+','+
				cast(@a1 as varchar(20))+','+
				cast(isnull(@a2,0) as varchar(4))+','+
				isnull(@a3,'')+','+
				isnull(@a4,'')+','+
				cast(@user_serial as varchar(20))+','+
				isnull(cast(@ac_money as varchar(20)),'')+','+
				cast(@aa7 as varchar(20))+','+
				cast(@rq as varchar(20))+','+
				cast(@sj as varchar(20))+','+
				isnull(cast(@jl_count as varchar(10)),'')+','+
				isnull(@a11,'')+','+
				isnull(@a12,'')+','+
				isnull(@a13,'')+','+
				isnull(@user_depname,'')+','+
				isnull(cast(@a15 as varchar(10)),'')+','+
				isnull(cast(@ac_subo as varchar(20)),0)+','+
				isnull(cast(@sub_money as varchar(20)),0)+','+
				isnull(cast(@a18 as varchar(20)),0)+','+
				isnull(cast(@discount_rate as varchar(20)),'')+','+
				isnull(cast(@ac_each as varchar(10)),0)+','+
				isnull(cast(@a21 as varchar(10)),0)+','+
				isnull(cast(@each_unit as varchar(10)),0)+','+
				'0'+','+
				'0'+','+
				'0'+','+
				'0'+','+
				cast(@a27 as varchar(10))+','+
				isnull(@a28,'')+','+
				cast(isnull(@a29,0) as varchar(10)) +','+
				isnull(@user_no,'')+','+
				isnull(@user_lname,'')
			--select @jl_data
			set @jl_number=@out_put
			update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number ,jl_bh=@a4
			where  dev_serial=@a3

			--select @out_put,@jl_data
			update st_device set add_sj=@current_date where bh=@dev_serial;



		end

end
end
else if(@yich=1)  --异常状态返回
begin

	set @jl_data='3100'+','+
        cast(@out_put as varchar(10))+','+
        cast(@a27 as varchar(10))+','+
        isnull(@a1,0)+','+
        isnull(cast(@user_serial as varchar(20)),0)+','+
        isnull(@a4,0)+','+
        isnull(cast(@aa7 as varchar(20)),0)

	set @jl_number=@out_put
	update xf_jl_yc set jl_data=@jl_data,jl_number=@jl_number
	where user_serial=@user_serial and dev_serial=@a3

	--select @out_put,@jl_data
	set @res=@jl_data
	--return
	--update st_device set add_sj=@current_date where bh=@dev_serial;
end

--if (@@error>0)
--begin
--	rollback
--end
--else
--begin
--	commit transaction
	set @res=@jl_data
	return
--end




end


else if @cmd_bz=5100		--冲正充值
begin


----------------------------------------------------异常状态报告
set @a=charindex(',',@remain_data)
set @style=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @card_hao=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @user_serial=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @jl_bh=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @dev_serial2=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @rq=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @sj=left(@remain_data,@a-1)

set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)
set @a=charindex(',',@remain_data)
set @pay_type=left(@remain_data,@a-1)

set @new_money=substring(@remain_data,@a+1,len(@remain_data)-@a)


--set @param=substring(@param,@a+1,len(@param)-@a)
--set @a=charindex(',',@param)
--set @new_each=left(@param,@a-1)

--set @param=substring(@param,@a+1,len(@param)-@a)
--set @a=charindex(',',@param)
--set @each_del=left(@param,@a-1)

--set @param=substring(@param,@a+1,len(@param)-@a)
--set @user_password=@param

------判断使用哪个设备编号---------
set @dev_serial1='001'+right('0000'+@dev_serial,4)
set @dev_serial2='001'+right('0000'+@dev_serial2,4)

if((select count(bh) from st_device where bh=@dev_serial2)=0)
begin
	set @dev_serial=@dev_serial1
end
else
begin
	set @dev_serial=@dev_serial2
end

/*
select @add_gly=a.user_no
from dt_addgly a
inner join dt_card b on a.user_no=b.gly_no
where right('0000000000000000'+b.card_hao,16)=right('0000000000000000'+@card_hao,16)
*/

---------------------------------记录时间
set @jl_sj=cast(substring(@rq,1,4)+'-'+substring(@rq,5,2)
+'-'+substring(@rq,7,2)+' '+substring(@sj,1,2)
+':'+substring(@sj,3,2)+':'+substring(@sj,5,2) as datetime)

----------------------------------------------------------
--begin transaction
--SET XACT_ABORT ON	--产生运行时错误，整个事务将终止并回滚
set @current_date=getdate()
if(@style!=113) --卡方式
begin
    select @user_serial=a.user_serial,  --人员序号
			@jl_count=a.jl_count+1
	from dt_ac_link a with(nolock)
	inner join dt_card b on a.user_serial=b.user_serial   --卡号表
	where b.card_hao=@card_hao
end
else if(@style=113)	--指纹消费
begin
    select @user_serial=a.user_serial,  --人员序号
			@jl_count=a.jl_count+1
	from dt_ac_link a with(nolock)
	where a.user_serial=@user_serial
	--inner join dt_card b on a.user_serial=b.user_serial   --卡号表
end
-------------------------------------------------------
set @back_ok=1
set @out_put=0
if(@new_each='' or @new_each is null)
begin
	set @new_each=0
end

select @state_flag=state,@card_hao_dev=user_card, @a2_dev=lx, @a3_dev=dev_serial,@a4_dev=jl_bh,
       @a5_dev=user_serial,@aa7_dev=more_money,@aa21_dev=each_new,@a27_dev=ident_type,@a29_dev=ac_pass,@jl_data =jl_data,
	   @ac_blocks=0,@ac_blockm=isnull(new_money,0),@ac_blocke=isnull(each_new,0),@add_gly=add_gly
from xf_jl_yc where dev_serial=cast(@dev_serial2 as int)  --设备请求状态表

if(@state_flag=1)
begin
	if (@user_serial = @a5_dev  and @jl_bh = @a4_dev and @new_money = @aa7_dev
        and @style = @a27_dev ) --正常的撤销
	begin
		insert into xf_jl
			(mould,lx,jl_bh,jl_sj,sam_serial,old_money,new_money,jl_count,dev_serial,user_serial,
			card_serial,sj,user_card,sub_xh,state,type,time_order,time_no,del_type,
			sub_old,sub_new,each_old,each_new,each_unit,more_money,discount_rate)
		select isnull(mould,0),2,jl_bh,@jl_sj,sam_serial,old_money,-new_money,@jl_count,dev_serial,user_serial,
			card_serial,@current_date,user_card,sub_xh,0,type,time_order,time_no,del_type,
			0,0,0,0,isnull(each_unit,0),more_money,isnull(discount_rate,0)
		from xf_jl_yc where dev_serial=cast(@dev_Serial as int)

		insert into xf_mx(mould,lx,type,jl_bh,dev_serial,user_serial,dep_serial,user_card,card_serial,rq,jl_sj,
			old_money,more_money,new_money,rate_money,discount_rate,money_count,
			new_sub,sub_count,new_add,add_count,new_del,del_count,new_zero,del_zero,new_edit,del_edit,new_each,each_unit,
			save_money,jl_count,dev_time_no,time_no,sj,gly_no)
	   select 0,2,1,jl_bh,@dev_serial,user_serial,null,null,card_serial,convert(varchar(20),@jl_sj,23),@jl_sj,
			old_money,0,0,0,discount_rate,0,
			0,0,-new_money,1,0,0,0,0,0,0,each_new,isnull(each_unit,0),
			old_money-new_money,jl_count,isnull(time_order,'0000000000000000'),isnull(time_no,'0000000000000000'),@current_date
			,@add_gly
		from xf_jl_yc
		where dev_serial=cast(@dev_Serial as int)

		insert into xf_addmoney(user_serial,input_money,add_money,gly_money,sj,update_sj,add_state,gly_no,type)
		select @user_serial,-new_money,-new_money,0,@jl_sj,getdate(),2,@add_gly,1
		from xf_jl_yc where user_serial=@user_serial and dev_serial=cast(@dev_Serial as int)

		update dt_ac_link set
			ac_money=ac_money-@ac_blockm,
			ac_addo=ac_addo-@ac_blockm,
			jl_count=jl_count+1
		where user_serial=@user_serial

		update dt_ac_user set
			ac_money=ac_money-@ac_blockm,
			ac_addo=ac_addo-@ac_blockm
		where user_serial=@user_serial

		update xf_jl_yc set state=0 where dev_serial=cast(@dev_serial as int)
	end
	else
	begin	--不正常的撤销
		insert into xf_yc(dev_serial,jl_bh,user_Serial,card_hao,lx,state_flag,new_money,new_each,ident_type,pass,
			make_lx,t_level,style_yc,yc_sj)
		values(@a3_dev,@a4_dev,@a5_dev,@card_hao_dev,@a2_dev,@state_flag,@aa7_dev,@aa21_dev,@a27_dev,@a29_dev,
			2,2,2,@current_date)
	end



end

--select 0,'5000'+','+cast(@out_put as varchar(10))+','+cast(@style as varchar(10))+','+isnull(@card_hao,'')+','
--	+cast(isnull(@user_serial,'') as varchar(20))+','+isnull(@jl_bh,'')

--if (@@error>0)
--begin
--	rollback
--end
--else
--begin

	--commit transaction
	set @res='5100'+','+cast(@out_put as varchar(10))+','+cast(@style as varchar(10))+','+isnull(@card_hao,'')+','
	+cast(isnull(@user_serial,'') as varchar(20))+','+isnull(@jl_bh,'')
	return
--end



end

else if @cmd_bz=6100
begin

	declare @old_password varchar(10)	--原密码
	declare @new_password varchar(10)	--新密码
	set @yich=0
	set @current_date=getdate()
	set @a=charindex(',',@remain_data)
	set @command_type=left(@remain_data,@a-1)
	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

	set @a=charindex(',',@remain_data)
	set @card_hao=left(@remain_data,@a-1)
	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

	set @a=charindex(',',@remain_data)
	set @user_serial=left(@remain_data,@a-1)
	set @remain_data=substring(@remain_data,@a+1,len(@remain_data)-@a)

	set @a=charindex(',',@remain_data)
	set @old_password=left(@remain_data,@a-1)
	set @new_password=substring(@remain_data,@a+1,len(@remain_data)-@a)
	set @out_put=0

	set @card_hao2=right('0000000000000000'+@card_hao,16)
	if(@command_type=113) --指纹方式
	begin
		select
			@user_serial2=a.user_serial,	--人员序号
			@ac_type=c.ac_type,             --账户类型
			@ac_state=a.ac_state,           --账户状态，0:启用 1:停用
			@pass_max=d.pass_max,            --密码是否启用
			@pass_word=c.ac_pass,            --密码
			@ac_kssj=c.ac_kssj,
			@ac_jssj=c.ac_jssj
			from dt_ac_link a with(nolock)
			inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
			inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表，取出折数
			inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
			where a.user_serial=@user_Serial     --人员序号

		if(@user_serial2 is null)    --人员不存在
		begin
			set @yich=1 --返回异常信息
			set @out_put=7
		end
	end
	else if (@yich!=1) --卡消费
	begin
		select 	@user_serial2=a.user_serial,  --人员序号
				@ac_state=a.ac_state,           --账户状态，0:启用 1:停用
				@pass_max=d.pass_max,            --密码是否启用
				@pass_word=c.ac_pass,            --密码
				@card_type=b.card_type,          --卡状态：0：正常，1：挂失
				@ac_kssj=c.ac_kssj,
				@ac_jssj=c.ac_jssj
			from dt_ac_link a with(nolock)
			inner join dt_card b on a.user_serial=b.user_serial   --卡号表
			inner join dt_ac_user c with(nolock) on a.user_serial=c.user_serial  --人员账户表 取出账户类型
			inner join dt_Ac_type d on c.ac_type=d.ac_bh            --账户类型表
			inner join dt_user a2 on a2.user_serial=c.user_serial   --人员表 取出部门编号
			where right('0000000000000000'+b.card_hao,16)=@card_hao2
		----------------------------------------------------------------------人员信息预判断

		if(@user_serial2 is null)    --卡不存在
		begin
			set @yich=1     --返回异常信息
			set @out_put=6  --无此卡号
		end
		else if(@card_type=1)
		begin
			set @yich=1
			set @out_put=5 --挂失
		end
	end
	set @dev_serial='001'+right('0000'+@dev_serial,4)
	---判断系统中是否存在对应的联机消费设备
	if((select count(bh) from st_device where bh=@dev_serial and dev_lb=6)=0)
	begin
	set @yich=1
	set @out_put=24		--无此设备
	end
	------------------------
	--增加判断无权限卡，场所未指定白名单的卡 1.取设备对应场所的白名单(带继承关系)；2.

	select @dep_serial0=(select acdep_serial from dt_dev where dev_serial=@dev_serial and lx=3)
	select @dep_serial2=(select dep_serial from dt_ac_dep where dep_serial=@dep_serial0)
	select @dep_parent=(select dep_parent from dt_ac_dep where dep_serial=@dep_serial2)
	if(@yich!=1)
	begin
	--create table #temp(
	--xh int identity(1,1),
	--serial bigint
	insert into #temp(serial)
		select distinct user_serial from
		(
		select  user_serial
		from dt_ac_dep_user
		where dep_serial=@dep_serial2
		and isnull(module_id,'0035')='0010'
		union all
		select  a.user_serial
		from dt_user a
		inner join online_xf_ac_dep b on a.user_dep=b.dep_serial
		inner join dt_ac_dep f on b.ac_serial=f.dep_serial
		where f.dep_serial=@dep_serial2
		) a

	select @xh=(select count(xh) from #temp)  --取人员的个数，以判断是否存在白名单


	if((@xh!=0) and (select count(xh) from #temp where serial=@user_serial2)=0)
	begin
	set @yich=1
	set @out_put=26
	end
	truncate table #temp
	end

	------------------------
	if(@yich!=1)
	begin
		--账户未启用 --在此为账户注销 账户状态：0：启用，1：停用
		--@ac_kssj datetime           --人员账户开始时间
		--@ac_jssj datetime           --人员账户结束时间
		if (@ac_state = 1)
		begin
			set @yich=1
			set @out_put=2 --账户停用
		end
		else if(convert(varchar(20),@ac_jssj,112) < convert(varchar(20),@current_date,112) or (convert(varchar(20),@ac_kssj,112)) > (convert(varchar(20),@current_date,112)))
		begin
			set @yich=1
			set @out_put=3 --人员账户不在有效期
		end
		else if(@pass_max=2)
		begin
			set @yich=1
			set @out_put=9
		end
		else if(@pass_word!=@old_password)
		begin
			set @yich=1
			set @out_put=23
		end
	end
	if(@yich!=1)
	begin
	update dt_ac_user set ac_pass=@new_password where user_serial=@user_serial2
	set @res='6100'+','+cast(@out_put as varchar(10))+','+cast(@cmd_bz as varchar(10))+','+cast(@out_put as varchar(10))+','+cast(@command_type as varchar(20))+','+cast(isnull(@card_hao,'') as varchar(20))+','+cast(isnull(@user_serial2,'') as varchar(20))
	end
	else if(@yich=1)
	begin
	set @res='6100'+','+cast(@out_put as varchar(10))+','+cast(@cmd_bz as varchar(10))+','+cast(@out_put as varchar(10))+','+cast(@command_type as varchar(20))+','+cast(isnull(@card_hao,'') as varchar(20))+','+cast(isnull(@user_serial2,'') as varchar(20))
	end
	return


end